你会想:“这个问题已经解决了很多很多次了,那他为什么不用谷歌呢?”请相信我,我什么都试过了。自上周以来,我一直在处理这个问题。我阅读了很多博客并搜索了 MSDN。但我不明白。
所以问题来了。有一个 Silverlight 4 应用程序和一个 WCF 数据服务,两者都在 localhost
上运行。
这是代码。我想没什么特别的。
private void InitializeData()
{
var query = (from item in ObjCtx.TestTgt
select item) as DataServiceQuery<TestTgt>;
Debug.Assert(query != null, "'query' is null");
query.BeginExecute(OnLoadDataFinished, query);
}
private void OnLoadDataFinished(IAsyncResult ar)
{
try
{
var query = ar.AsyncState as DataServiceQuery<TestTgt>;
Debug.Assert(query != null, "'query' is null");
var res = query.EndExecute(ar).ToList();
Data.Data = new ObservableCollection<TestTgt>(res);
}
catch(Exception ex)
{
Data.StateDescription = String.Format("Exception occured.{0}{0}{1}", Environment.NewLine, AgExMsgFormatter.GetExText(ex));
}
}
在 OnLoadData
这一行:var res = query.EndExecute(ar).ToList();
发生以下异常。
Exception occured.
[EXCEPTION]
[TYPE:] 'InvalidOperationException'
[MESSAGE:] 'An error occurred while processing this request.'
[CALLSTACK:]
at System.Data.Services.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)
at System.Data.Services.Client.QueryResult.EndExecute[TElement](Object source, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)
at SimpleGrid.SimpleGridVm.OnLoadDataFinished(IAsyncResult ar)[INNEREXCEPTION]
[TYPE:] 'SecurityException'
[MESSAGE:] ''
[CALLSTACK:]
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)[INNEREXCEPTION]
[TYPE:] 'SecurityException'
[MESSAGE:] 'Security error.'
[CALLSTACK:]
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(Object sendState)
[/INNEREXCEPTION]
[/INNEREXCEPTION]
[/EXCEPTION]
作为身份验证模式,我想使用 Windows
。两者都在 IIS 中为此进行了配置。 Web 服务正在运行并提供正确的数据。
那我错过了什么?我认为这应该有效。任何帮助将不胜感激。
问候
请您参考如下方法:
您的问题看起来像是跨域错误:默认情况下不允许 Silverlight 应用程序执行跨域 Web 服务调用。这意味着如果您的 cassini 托管 SL 应用程序(例如域 localhost:4314)尝试访问域 localhost 上的 WCF 服务,调用将失败并出现安全异常。
您可以使用 fiddler 或 firebug 的网络选项卡(在 firefox 上)轻松捕获问题:该应用程序首先尝试访问 web 服务域根目录中名为“clientaccesspolicy.xml”的文件。此文件在 WCF 服务器上定义了一个策略,该策略授权服务器上的跨域调用。
这是一个跨域策略文件的示例,它允许任何 SL 应用程序访问该域上的任何网络服务:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
<policy >
<allow-from http-methods="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
不过你可以更具体一点。有关更多信息,请访问 MSDN :