Silverlight Crossdomain [英] Silverlight Crossdomain

查看:148
本文介绍了Silverlight Crossdomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多链接到MSDN和在我的机器上工作!答案,所以我想问我的问题与确切的步骤,以复制我在做什么。因为我们使用一个已经存在的web服务,我要求在一个Web服务的上下文托管在我的项目之外,不像许多教程和在线视频。所以这里:



***创建一个新的ASP.NET webservice项目。




在浏览器中查看,点击Invoke按钮。



在我的机器上,URL是: http:// localhost:15511 / WebSite5 / Service.asmx


***启动Visual Studio的新实例,创建Silverlight Web应用程序项目。



***在其上粘贴单个按钮,使用事件处理程序调用Web服务。我个人nuke网格和使用一个简单的StackPanel。例如

 < UserControl x:Class =SilverlightApplication1.Page
xmlns =http:// schemas .microsoft.com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Width =400Height = 300>
< StackPanel>
< Button Click =Button_Click>
< Button.Content>
< TextBlock Text =Test/>
< /Button.Content>
< / Button>
< / StackPanel>
< / UserControl>

使用Button_Click的语句和事件处理程序添加Web引用:

  private void Button_Click(object sender,RoutedEventArgs e)
{
ServiceSoapClient client = new ServiceSoapClient
client.HelloWorldCompleted + =(object s,HelloWorldCompletedEventArgs ea)=> {
MessageBox.Show(ea.Result);
};
client.HelloWorldAsync();
}





运行,当然,由于跨域问题,它会爆炸。接下来,将 clientaccesspolicy.xml 文件添加到托管该服务的Web应用程序的根目录中:

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

这应该打开,因为它有一个通配符的头,uris和资源, / p>


  • 再次运行时会出现错误:



尝试向URI http:// localhost:15511 / WebSite5 / Service.asmx 。这可能是由于尝试以跨域方式访问服务,而没有适当的跨域策略或不适合SOAP服务的策略。您可能需要与服务所有者联系,以发布跨域策略文件,并确保允许发送与SOAP相关的HTTP标头。



所以问题:有clientaccesspolicy文件的秘密吗?可以尝试使用crossdomain.xml,但是它给出了类似的结果。

解决方案

的时间。在过去,我已经解决了这个使用Web应用程序作为启动,但它看起来像你已经做到了。



我的主题主题: http://www.donnfelker.com/silverlight-cross-domain-issue/


I've seen a lot of links to MSDN and "works on my machine!" answers so I'd like to ask my question with the exact steps to duplicate what I'm doing. Because we are using an already existing webservice, I'm asking with the context of having a webservice hosted outside of my project, unlike many of the tutorials and videos online. So here goes:

*** Create a new ASP.NET webservice project.

It will come with an existing Service.asmx file exposing a "HelloWorld" web method.

View in browser, hit the "Invoke" button. It should work returning the "Hello World" string.

On my machine, the URL is: "http://localhost:15511/WebSite5/Service.asmx"

*** Start a new instance of Visual Studio, create a Silverlight Web Application Project.

*** Stick a single button on there with an event handler to call the web service. I personally nuke the Grid and use a simple StackPanel. eg.

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <StackPanel>
        <Button Click="Button_Click">
            <Button.Content>
                <TextBlock Text="Test"/>
            </Button.Content>
        </Button>
    </StackPanel>
</UserControl>

Add the web reference, using statement and event handler for the Button_Click:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ServiceSoapClient client = new ServiceSoapClient();
        client.HelloWorldCompleted += (object s, HelloWorldCompletedEventArgs ea) => { 
            MessageBox.Show(ea.Result); 
        };
        client.HelloWorldAsync();
    }

Run and of course it blows up because of crossdomain issues. So next add the clientaccesspolicy.xml file with the following to the root of your web application hosting the service:

<?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 include-subpaths="true" path="/"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

This should open things up since it's got a wildcard for headers, uris, and resources, right?

  • Run again and you get an error:

An error occurred while trying to make a request to URI 'http://localhost:15511/WebSite5/Service.asmx'. 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.

So question: is there a secret to the clientaccesspolicy file? One could alternately try with the crossdomain.xml but it gives a similar result.

解决方案

I've had this same problem a couple of times. In the past I've solved this by using the Web App as start up, but it looks like you've already done that.

My post on the subject: http://www.donnfelker.com/silverlight-cross-domain-issue/

这篇关于Silverlight Crossdomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆