我正在尝试在 Silverlight 5 应用程序/.NET 4.0 应用程序中使用 WCF 数据服务。当我手动查询服务时,我可以在 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
}
}
}
完整的堆栈跟踪(堆栈跟踪 1):
System.InvalidOperationException 未被用户代码处理 Message=处理此请求时发生错误。StackTrace:在 System.Data.Services.Client.BaseAsyncResult.EndExecute[T](对象源,字符串方法,IAsyncResult asyncResult)在 System.Data.Services.Client.QueryResult.EndExecute[TElement](对象源,IAsyncResult asyncResult)在System.Data.Services.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, IAsyncResult asyncResult) at System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult) at ThreatModeler.Silverlight.ProjectWindow.OnCategoriesFecthComplete (IAsyncResult ar) 在 System.Data.Services.Client.BaseAsyncResult.HandleCompleted() 在 System.Data.Services.Client.QueryResult。DisplayClass1.b _0(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c_ DisplayClass1a.b_18(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem .ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() InnerException: System.Security.SecurityException 消息=安全错误。StackTrace:在 System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Data.Services.Client 的 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)。
完整的堆栈跟踪(堆栈跟踪 #02)
{System.InvalidOperationException:处理此请求时发生错误。---> 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) --- 结束内部异常堆栈跟踪 --- 在 System.Data.Services.Client.QueryResult.EndExecute[TElement](Object source, String method, IAsyncResult asyncResult) at System.Data.Services.Client.QueryResult.EndExecute[T](Object源,IAsyncResult asyncResult)在 System.Data.Services.Client.DataServiceRequest.EndExecute[TElement](对象源,DataServiceContext 上下文,System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult) 在 System.Data.Services.Client.BaseAsyncResult 的 System.Data.Services.Client.BaseAsyncResult.HandleCompleted() 0(IAsyncResult s)。<>c _DisplayClass1 .b_ 0(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClass1a.b__18(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading .IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}