System.Security.SecurityException:尝试在Silverlight 5应用程序中使用WCF数据服务时出现安全性错误

Ste*_*son 1 .net-4.0 wcf-data-services odata silverlight-5.0 c#-4.0

我试图在Silverlight 5应用程序/ .NET 4.0应用程序中使用WCF数据服务.当我手动查询服务时,我可以在Web浏览器中看到Atom提要的数据.

但是,当我尝试通过标准方法查询数据时,我收到以下错误:

{System.Security.SecurityException:安全错误.位于System.Data.Services.Client.Hespse上的System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)(IAsyncResult asyncResult),System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)上的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
        }

    }
Run Code Online (Sandbox Code Playgroud)

}

完整堆栈跟踪(堆栈跟踪1):

用户代码未处理System.InvalidOperationException消息=处理此请求时发生错误.StackTrace:System.Data.Services.Clase.BaseAsyncResult.EndExecute [T](对象源,String方法,IAsyncResult asyncResult),位于System.Data.Services.Client.QueryResult.EndExecute [TElement](对象源,IAsyncResult asyncResult)at at System.Data.Services.Client.DataServiceRequest.EndExecute [TElement](对象源,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(对象状态).在System.Threading.ThreadPool.ThreadPool上运行System.Threading.Qhreading.ThreadPoolWorkIue.ExecuteWorkItem(),System.Threading.ThreadPoolWaitCallback.PerformWaitCallback(),在System.Threading.ThreadPoolWorkItem.ExecuteWorkItem()处运行(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) InnerException:System.Security.SecurityException Message =安全性错误.StackTrace:位于System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult),位于System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)InnerException:

完整堆栈跟踪(堆栈跟踪#02)

{System.InvalidOperationException:处理此请求时发生错误.---> System.Security.SecurityException:安全性错误.位于System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)处于System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)---结束内部异常堆栈跟踪---在System.Data.Services.Client.BaseAsyncResult.EndExecute [T](对象源,String方法,IAsyncResult asyncResult)处于System.Data.Services.Client.QueryResult.EndExecute [TElement](对象)源,IAsyncResult的asyncResult)在System.Data.Services.Client.DataServiceRequest.EndExecute [TElement](对象源,DataServiceContext上下文,IAsyncResult的asyncResult)在System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult的asyncResult)在ThreatModeler .Silverlight.ProjectWindow.b_ 0(IAsyncResult的多个)在System.Data.Services.Client.BaseAsyncResult.HandleCompleted()在System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult的asyncResult)在System.Data.Services.Cl ient.BaseAsyncResult.<>Ç _DisplayClass1.b_ 0(IAsyncResult的asyncResult)在System.Net.Browser.ClientHttpWebRequest.<>Ç _DisplayClass1a.b__18(对象STATE2)在System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(对象状态)在的System.Threading .ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,Boolean preserveSyncCtx)位于System.Threading.ThreadPoolWork.ThreadPool上的System.Threading.ThreadPoolWorkItem.ExecuteWorkItem()处于System.Threading.ThreadPoolWork.ThreadPool.ThreadPool上的System.Threading.ThreadPoolWorkQueue.Dispatch(). PerformWaitCallback()}

Jos*_*ham 5

这对我有用.在我的服务中

  1. 选择Installed Templates下的Data,然后选择XML File.将文件命名为clientaccesspolicy.xml.
  2. 为文件输入以下内容:
<?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>
Run Code Online (Sandbox Code Playgroud)