Silverlight SL 4和WCF数据服务:InvalidOperationException->;安全例外

Silverlight SL 4和WCF数据服务:InvalidOperationException->;安全例外,silverlight,silverlight-4.0,wcf-data-services,Silverlight,Silverlight 4.0,Wcf Data Services,你会想:“这个问题已经解决了很多很多次。那他为什么不使用谷歌呢?”请相信我,我什么都试过了。我从上周开始就在处理这个问题。我读了很多博客,搜索了MSDN。但我不明白 这就是问题所在。有一个Silverlight 4应用程序和一个WCF数据服务,它们都在localhost上运行 这是代码。我想没什么特别的 private void InitializeData() { var query = (from item in ObjCtx.TestTgt

你会想:“这个问题已经解决了很多很多次。那他为什么不使用谷歌呢?”请相信我,我什么都试过了。我从上周开始就在处理这个问题。我读了很多博客,搜索了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));
            }
        }
private void InitializeData()
{
变量查询=(来自ObjCtx.TestTgt中的项
选择项)作为DataServiceQuery;
Assert(query!=null,“'query'为null”);
BeginExecute(OnLoadDataFinished,query);
}
私有void OnLoadDataFinished(IAsyncResult ar)
{
尝试
{
var query=ar.asynchState作为DataServiceQuery;
Assert(query!=null,“'query'为null”);
var res=query.EndExecute(ar.ToList();
数据。数据=新的可观测集合(res);
}
捕获(例外情况除外)
{
Data.statescription=String.Format(“发生异常。{0}{0}{1}”,Environment.NewLine,AgExMsgFormatter.getextextext(ex));
}
}
在这一行的
OnLoadData
中:
var res=query.EndExecute(ar.ToList()发生以下异常

发生异常

[例外]
[类型:'InvalidOperationException'
[消息:'处理此请求时出错。'
[调用堆栈:
位于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](对象源、DataServiceContext上下文、IAsyncResult asyncResult)
在System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)
在SimpleGrid.SimpleGridVm.OnLoadDataFinished(IAsyncResult ar)

[内部异常]
[类型:'SecurityException'
[信息:”
[调用堆栈:
位于System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)
在System.Net.Browser.ClientHttpWebRequest.EndGetResponse中(IAsyncResult asyncResult)
位于System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
位于System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)

[内部异常]
[类型:'SecurityException'
[消息:'安全错误。
[调用堆栈:
位于System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
在System.Net.Browser.ClientHttpWebRequest.c__中显示Class5.b__4(对象发送状态)
在System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)
[/INNEREXCEPTION]
[/INNEREXCEPTION]
[/例外]

作为身份验证模式,我想使用
Windows
。两者都是在IIS中为此配置的。web服务正在运行并正在提供正确的数据

那么我错过了什么?我认为这应该行得通。任何帮助都将不胜感激


问候

嗯,好吧,这很令人惊讶,但我明白了。问题不在于身份验证或类似的东西。这是调试器。该web服务未托管在Silverlight应用程序的网站上。因此,web服务调用失败了

在浏览器中加载已部署的站点时,一切都非常正常。对我来说,这似乎有些奇怪,但也许有人能给我一个很好的解释这种行为

但这带来了另一个问题。当Silverlight应用程序引用外部web服务时,如何调试它。也许我会为此打开一个单独的线程



关于

您可能需要以管理员身份启动Visual Studio

您的问题看起来像是跨域错误:默认情况下,silverlight应用程序不允许执行跨域Web服务调用。这意味着,如果您的cassini托管的SL应用程序(例如域localhost:4314)尝试访问域localhost上的WCF服务,则调用将失败,并出现安全异常

通过使用fiddler或firebug(在firefox上)的网络选项卡,您可以很容易地发现这个问题:应用程序首先尝试访问webservice域根上名为“clientaccesspolicy.xml”的文件。此文件在WCF服务器上定义了一个策略,用于授权服务器上的跨域调用

以下是跨域策略文件的示例,它允许任何SL应用程序访问此域上的任何Web服务:

<?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>

这里是我的解决方案:

  • 正如您所说,RIA服务在本地主机上运行
  • 当Silverlight项目是statup项目时,会出现错误
  • 在这种情况下,RIA服务是在不同的上下文中调用的 网页
  • VisualStudio调试器对此发出警告,但弹出窗口通常是 选中“不显示”
  • 解决办法是:

  • 将解决方案中的Web项目设置为启动项目
  • 将生成的TestPage.html设置为起始页

  • RIA服务也可以工作。

    请阅读另一篇文章Silverlight 4:InvalidOperationException(SecurityException)WCF数据服务[Silverlight 4:InvalidOperationException(SecurityException)WCF数据服务]关于