C# 从Silverlight调用REST服务
我正在为我们的内部网构建一个Silverlight 4.0 web应用程序,它将通过REST连接到服务器以获取数据。我正在使用VisualStudio2010 我在方法中使用以下代码来发出请求:C# 从Silverlight调用REST服务,c#,.net,silverlight,rest,C#,.net,Silverlight,Rest,我正在为我们的内部网构建一个Silverlight 4.0 web应用程序,它将通过REST连接到服务器以获取数据。我正在使用VisualStudio2010 我在方法中使用以下代码来发出请求: var wc = new WebClient(); const string uri = "http://server/api/statistics.svc/overall/"; wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_
var wc = new WebClient();
const string uri = "http://server/api/statistics.svc/overall/";
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(uri, UriKind.Absolute));
我的OnReadCompleted事件处理程序:
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var buffer = new byte[e.Result.Length];
e.Result.Read(buffer,0,(int)e.Result.Length);
var xmlstr = buffer.Aggregate(String.Empty, (current, t) => current + (char) t);
// Do something with xmlstr...
}
我的问题出现在尝试运行应用程序时。异常在wc_OpenReadCompleted事件处理程序的第1行抛出,并具有以下详细信息:
TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details.
内部异常:
System.Security.SecurityException: Security error.
我的REST服务不使用任何类型的身份验证
我已使用以下设置创建了位于的clientaccesspolicy.xml:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/api/statistics.svc/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
非常感谢你的帮助
谢谢
杰弗里·凯文·普里
更新
我没有在xml文件中指定/api/。问题解决了
谢谢 您的Silverlight应用程序只允许连接到从中下载的服务器以及具有跨域策略文件的服务器。这就是我们所知道的。由于安全原因,与任何其他服务器的连接都被拒绝。这是正确的,我的clientaccesspolicy.xml位于。查看上面的内容我没有在clientaccesspolicy.xml中把api放在服务前面。。。我会给你的,因为你的帖子让我重新审视它。。。谢谢