SL 4和WCF DataService:InvalidOperationException - >抛出:SecurityException

时间:2010-06-01 09:45:05

标签: silverlight silverlight-4.0 wcf-data-services

你会想:“这个问题已经解决了很多次。那他为什么不用谷歌?”拜托,相信我,我尝试了一切。我从上周开始就处理这个问题了。我阅读了很多博客并搜索了MSDN。但是我没理解。

所以这就是问题所在。有一个Silverlight 4应用程序和一个WCF DataService,它们都在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();发生以下异常。

  

发生异常。

     

[例外]
  [TYPE:]'InvalidOperationException'
  [消息:]'处理此请求时出错。'
  [CALLSTACK:]
     at System.Data.Services.Client.BaseAsyncResult.EndExecute [T](Object source,String method,IAsyncResult asyncResult)
     在System.Data.Services.Client.QueryResult.EndExecute [TElement](对象源,IAsyncResult asyncResult)
     at System.Data.Services.Client.DataServiceRequest.EndExecute [TElement](对象源,DataServiceContext上下文,IAsyncResult asyncResult)
     在System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)
     在SimpleGrid.SimpleGridVm.OnLoadDataFinished(IAsyncResult ar)

     

[的InnerException]
  [TYPE:]'SecurityException'
  [消息:]''   [CALLSTACK:]
     在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)
     在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     在System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     在System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)

     

[的InnerException]
  [TYPE:]'SecurityException'
  [消息:]'安全错误。'   [CALLSTACK:]
     在System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
     在System.Net.Browser.ClientHttpWebRequest。&lt;&gt; c__DisplayClass5.b__4(Object sendState)
     在System.Net.Browser.AsyncHelper。&lt;&gt; c__DisplayClass2.b__0(Object sendState)
  [/的InnerException]
  [/的InnerException]
  [/异常]

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

那我错过了什么?我认为这应该有效。任何帮助将不胜感激。

此致

4 个答案:

答案 0 :(得分:4)

您的问题看起来像是跨域错误:默认情况下,不允许Silverlight应用程序执行跨域Web服务调用。这意味着,如果您的cassini托管SL应用程序(例如,domain 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>

但是你可以更具体一点。有关详情,请访问MSDN

答案 1 :(得分:2)

嗯,好吧,这太令人惊讶,但我明白了。问题不在于身份验证或类似的事情。这是调试器。 Web服务不托管在Silverlight应用程序的Web站点上。因此,Web服务调用失败。

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

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


此致

答案 2 :(得分:1)

这是我的解决方案:

  1. RIA服务正如您所说,在localhost上运行。
  2. 当Silverlight项目是statup项目时出现错误。
  3. 在这种情况下,在不同的情况下调用RIA服务 网页。
  4. Visual Studio Debugger警告说,但弹出窗口经常出现 检查“不要显示”。
  5. 解决方案是:

    1. 将解决方案中的Web项目设置为启动项目。
    2. 将生成的TestPage.html设置为起始页。
    3. RIA服务有效。

答案 3 :(得分:0)

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

相关问题