Silverlight 跨域 [英] Silverlight Crossdomain

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

问题描述

我看到了很多指向 MSDN 和在我的机器上工作!"的链接.答案所以我想用确切的步骤来问我的问题来复制我正在做的事情.因为我们正在使用一个已经存在的网络服务,所以我问的是在我的项目之外托管网络服务的上下文,这与许多在线教程和视频不同.所以这里是:

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:

*** 创建一个新的 ASP.NET 网络服务项目.

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

它将带有一个现有的 Service.asmx 文件,公开一个HelloWorld"网络方法.

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

在浏览器中查看,点击调用"按钮.它应该可以返回Hello World"字符串.

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

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

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

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

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

*** 在上面贴一个带有事件处理程序的按钮来调用 Web 服务.我个人使用了 Grid 并使用了一个简单的 StackPanel.例如.

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

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

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();
    }

  • 运行 Silverlight 应用程序.就我而言,我将前往我的 Silverlight 测试页面:http://localhost:15558/SilverlightApplication1TestPage.aspx
  • 运行,当然它会因为跨域问题而爆炸.因此,接下来将带有以下内容的 clientaccesspolicy.xml 文件添加到托管服务的 Web 应用程序的根目录:

    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>
    

    这应该会打开,因为它有一个用于标头、uri 和资源的通配符,对吧?

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

    • 再次运行,你得到一个错误:

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

    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.

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

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

    推荐答案

    我遇到过几次同样的问题.过去我通过使用 Web App 作为启动来解决这个问题,但看起来你已经这样做了.

    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.

    我关于这个主题的帖子:http://www.donnfelker.com/silverlight-跨域问题/

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

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

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