我正在尝试在Silverlight 5应用程序中使用WCF数据服务。NET 4.0应用程序。当我手动查询服务时,我可以在web浏览器中看到作为Atom提要的数据。
然而,当我试图通过标准方法查询数据时,我得到以下错误:
{System.Security。SecurityException:安全错误。在System.Net.Browser.ClientHttpWebRequest。EndGetResponse (IAsyncResult asyncResult)在System.Data.Services.Http.ClientHttpWebRequest。EndGetResponse (IAsyncResult asyncResult)在System.Data.Services.Client.QueryResult。AsyncEndGetResponse (IAsyncResult asyncResult)}
内部异常为空。所以目前还没有有意义的信息。
我的代码public partial class ProjectWindow : ChildWindow
{
XYZEntitis.XYZEntitis db = new XYZEntitis.XYZEntitis(new Uri("http://google.com/wcfdataservice1.svc/"));
DataServiceCollection<XYZEntitis.Label> dscCats = new DataServiceCollection<XYZEntitis.Label>();
CollectionViewSource customerAddressViewSource;
ObservableCollection<XYZEntitis.Label> labelsBindingCollection = new ObservableCollection<XYZEntitis.Label>();
public ProjectWindow()
{
InitializeComponent();
btnBack.IsEnabled = false;
//Load Project categories
BindProjectCategories();
}
public void BindProjectCategories()
{
#region Code
//#############################################
//Approach # 01 : Raises Security Exception
//#############################################
//DataServiceQuery<XYZEntitis.Label> query = db.Labels;
//query.BeginExecute(
// s =>
// {
// var state = s.AsyncState as DataServiceQuery<XYZEntitis.Label>;
// var data = new ObservableCollection<XYZEntitis.Label>();
// foreach (var entity in state.EndExecute(s))
// data.Add(entity);
// cbCategoryName.ItemsSource = data;
// }, query);
#endregion
//#############################################
//Approach No # 2 : 0 Records are returned
//#############################################
var cats = from cat in db.Labels select cat;
//cats.ToList();
dscCats.LoadCompleted += this.dscCats_LoadCompleted;
dscCats.LoadAsync(cats);
//#############################################
//Approach # 3 : Security Exception
//#############################################
//var query = from cat in db.Labels select cat;
//((DataServiceQuery<XYZEntitis.Label>)query).BeginExecute(OnCategoriesFecthComplete, query);
}
private void OnCategoriesFecthComplete(IAsyncResult ar)
{
var query = ar.AsyncState as DataServiceQuery<XYZEntitis.Label>;
// Get the response of the query.
var response = query.EndExecute(ar);
foreach (XYZEntitis.Label label in response)
{
labelsBindingCollection.Add(label);
}
cbCategoryName.ItemsSource = labelsBindingCollection;
}
private void dscCats_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
if (dscCats.Continuation != null)
{
dscCats.LoadNextPartialSetAsync();
}
else
{
cbCategoryName.ItemsSource = dscCats;
#region code
//var data = (DataServiceCollection<XYZEntitis.Label>)sender;
//cbCategoryName.ItemsSource = data;
//cbCategoryName.DisplayMemberPath = "Name";
//customerAddressViewSource =
// (CollectionViewSource)this.Resources["customerAddressViewSource"];
//customerAddressViewSource.Source = dscCats;
#endregion
}
}
}
完成堆栈跟踪(Stack Trace 1):
系统。InvalidOperationException未被用户代码处理消息=处理此请求时发生错误。加:在System.Data.Services.Client.BaseAsyncResult。endexexecute [T](对象源,字符串方法,asyncResult)在System.Data.Services.Client.QueryResult。EndExecute[element](对象源,asyncResult asyncResult)在System.Data.Services.Client.DataServiceRequest。endexexecute [element](对象源,DataServiceContext上下文,IAsyncResult asyncResult)在System.Data.Services.Client.DataServiceQuery 1。EndExecute (IAsyncResult asyncResult)在ThreatModeler.Silverlight.ProjectWindow。OnCategoriesFecthComplete (IAsyncResult ar)在System.Data.Services.Client.BaseAsyncResult.HandleCompleted ()在System.Data.Services.Client.QueryResult。AsyncEndGetResponse (IAsyncResult asyncResult)在System.Data.Services.Client.BaseAsyncResult灵活;> c_ DisplayClass1。b _0 (IAsyncResult asyncResult)在System.Net.Browser.ClientHttpWebRequest灵活;> c_ DisplayClass1a。b _18 state2(对象)在System.Threading.QueueUserWorkItemCallback。WaitCallback_Context(对象状态)在System.Threading.ExecutionContext。运行(ExecutionContext ExecutionContext, ContextCallback, callback, Object state, Boolean, preserveSyncCtx)在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem ()在System.Threading.ThreadPoolWorkQueue.Dispatch ()在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback ()InnerException: System.Security.SecurityException=安全错误消息。加:在System.Net.Browser.ClientHttpWebRequest。EndGetResponse (IAsyncResult asyncResult)在System.Data.Services.Http.ClientHttpWebRequest。EndGetResponse (IAsyncResult asyncResult)在System.Data.Services.Client.QueryResult。AsyncEndGetResponse (IAsyncResult asyncResult)InnerException:
完成堆栈跟踪(Stack Trace #02)
{系统。InvalidOperationException:处理过程中发生错误这个请求。--> System.Security.SecurityException:安全错误。System.Net.Browser.ClientHttpWebRequest.EndGetResponse (IAsyncResultasyncResult)System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse (IAsyncResultasyncResult)System.Data.Services.Client.QueryResult.AsyncEndGetResponse (IAsyncResult内部异常堆栈跟踪结束——atSystem.Data.Services.Client.BaseAsyncResult.EndExecute [T](对象来源,字符串方法,IAsyncResult asyncResult) atSystem.Data.Services.Client.QueryResult.EndExecute TElement(对象来源:IAsyncResult asyncResultSystem.Data.Services.Client.DataServiceRequest.EndExecute TElement(对象来源,DataServiceContext上下文,IAsyncResult asyncResult) at1. endexecute (IAsyncResult System.Data.Services.Client.DataServiceQuery"asyncResult)(IAsyncResult ThreatModeler.Silverlight.ProjectWindow.b_ 05)在system . data . services . client . baseasyncresult . handleccompleted ()在System.Data.Services.Client.QueryResult.AsyncEndGetResponse (IAsyncResultasyncResult)System.Data.Services.Client.BaseAsyncResult灵活;c> _DisplayClass1.b_ 0 (IAsyncResultasyncResult)System.Net.Browser.ClientHttpWebRequest灵活;c> _DisplayClass1a.b__18(对象state2)System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(对象在System.Threading.ExecutionContext.Run(ExecutionContext . stateexecutionContext, ContextCallback, callback,对象状态,布尔值preserveSyncCtx)System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem ()threadpoolworkqueue . dispatch (System.Threading._ThreadPoolWaitCallback.PerformWaitCallback ()}
这对我很有效。为我服务
- 在"Installed Templates"下选择"Data",选择"XML File"。将文件命名为clientaccessppolicy .xml。
- 在文件中输入如下内容:
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="http://*" /> <domain uri="https://*" /> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>