silverlight - Silverlight 和 WCF 服务的通信异常

标签 silverlight wcf azure securityexception

我有一个托管在 ASP Azure Web 角色中的 Silverlight 4 应用程序。 Web 角色公开 WCF 服务。 (所有这些都在同一个 Visual Studio 解决方案中。)

我成功添加了对服务的引用,并生成了客户端代码。但是,它会导致错误:

ExpenseServiceClient service = new ExpenseServiceClient();
service.GetExpensesCompleted += new EventHandler<GetExpensesCompletedEventArgs>(service_GetExpensesCompleted);
service.GetExpensesAsync();

回调:

    static void service_GetExpensesCompleted(object sender, GetExpensesCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.ToString());
            return;
        }

        // ...
    }

e.错误如下:

{System.ServiceModel.CommunicationException: An error occurred while trying to make a request to URI 'http://localhost:88/ExpenseService.svc'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute.

Please see the inner exception for more details.

---> System.Security.SecurityException --->
System.Security.SecurityException: Security error.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at ExpenseCalc_SilverLight.ExpenseService.ExpenseServiceClient.ExpenseServiceClientChannel.EndGetExpenses(IAsyncResult result)
   at ExpenseCalc_SilverLight.ExpenseService.ExpenseServiceClient.ExpenseCalc_SilverLight.ExpenseService.IExpenseService.EndGetExpenses(IAsyncResult result)
   at ExpenseCalc_SilverLight.ExpenseService.ExpenseServiceClient.OnEndGetExpenses(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)}   System.Exception {System.ServiceModel.CommunicationException}

这两个都在localhost上运行。我做错了什么?

最佳答案

Silverlight 期望每当进行被认为是“跨域”的调用时都会出现 ClientAccessPolicy.XML 文件(这样措辞是相当严格的)。你设置了吗?我不是 SL 专家,但当策略文件不存在时,通常会出现该错误。

简单的方法是在站点的根目录下创建一个,因此 http://localhost/clientaccesspolicy.xml

一个非常宽松的“允许所有”文件类似于:

<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers=""*"">
                <domain uri=""*""/>
            </allow-from>
            <grant-to>
                <resource path=""/"" include-subpaths=""true""/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

对于更复杂的场景(可能会出现在 Azure 中,具体取决于您的体系结构),您可以创建一个 IIS 处理程序来提供预期的 XML,从而允许根据请求动态创建策略。

如果您决定采用这种方法,有很多资源可以提供,但我建议您首先尝试简化一个方法,以确保这就是问题所在。

关于silverlight - Silverlight 和 WCF 服务的通信异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3901836/

相关文章:

c# - 默认 wcf 服务应用程序没有定义端点

git - 对 'other git' 存储库进行提交时,Azure DevOps 管道 CI 触发器不会调用

r - 运行 Azure 机器学习 Web 服务时出错,但实验运行正常

Web 应用程序的 Azure 市场合作伙伴

wpf - 如何将 View 中控件的 UI 调度程序传递给 ViewModel

c# - WCF - 如何在 C# 代码中指定端点的契约(Contract)?

Silverlight (3.0) : How to add cell padding to a Grid?

wcf - 在 .NET 中使用 JSON-RPC Web 服务

mysql - 用于 Windows Phone 远程数据库的 VB silverlight

silverlight - 如何在 XAML 中声明十进制值?