Silverlight Web服务错误跨域策略 [英] Silverlight Web Service Error Crossdomain Policy

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

问题描述



我正在尝试使用在另一个使用mysql数据库的项目中构建的asp.net Web服务.现在,我想将此服务添加到已添加的Silverlight应用程序中,当我运行此服务时,出现以下错误

<big>An error occurred while trying to make a request to URI ''http://netpeach102/webservices/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. 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.</big>



在Google中搜索后,我在WebService的bin文件夹中添加了clientaccespolicy.xml和crossdomain.xml,但仍然遇到相同的错误.

谢谢,

解决方案

访问第三方Web服务时应使用跨域策略文件.有一些方法可以开发它.我将说明一种可以解决您的问题的方法.

步骤1:创建IPolicyRetriever接口

[ServiceContract]
    public interface IPolicyRetriever
    {
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();
        [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
        Stream GetFlashPolicy();
    }



第2步:在您的服务中实现IPolicyRetriever接口

public class Service1 : IService1, IPolicyRetriever
    {
#region [ IPolicyRetriever Members ]
        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        public Stream GetSilverlightPolicy()
        {
            string result = @"<?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>";
            return StringToStream(result);
        }
        public Stream GetFlashPolicy()
        {
            string result = @"<?xml version=""1.0""?>
                            <!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
                            <cross-domain-policy>
                                <allow-access-from domain=""*"" />
                            </cross-domain-policy>";
            return StringToStream(result);
        }
        #endregion
}



蒂姆·豪雅(Tim Heuer)在此视频中描述了另一种方式:
http://www.silverlight.net /learn/videos/all/how-to-to-use-cross-domain-policy-files-with-silverlight/


您必须将文件放在正确的位置.当我开始工作时,我会告诉你我将它们放置在哪里.


它需要放入域的 ROOT 中.这很重要,因为它不是应用程序的根目录,而是根网站.
即使您的应用程序位于mySite.com/myapp,策略文件也必须位于mysite.com/clientaccesspolicy.xml.
参考:
http://timheuer.com/blog/存档/2008/04/06/silverlight-cross-domain-policy-file-snippet-intellisense.aspx [ http://www.netomatix.com/post/2010/03/09/Silverlight-Cross-Domain-Web-Service-Access-Error.aspx [

Hi,

I am trying to use a asp.net Web Service which is built in another project which is using mysql database . now i want to add this service to my silverlight application i have added and when i am running this i am getting the following error

<big>An error occurred while trying to make a request to URI ''http://netpeach102/webservices/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. 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.</big>



After Searching in google i have added clientaccespolicy.xml and crossdomain.xml in the bin folder of WebService still i am getting the same error.

Thanks,

解决方案

You should use cross-domain policy files when accessing third party web services. There are some ways to develop it; I explain a way that it can solve your problem.

Step 1: Create IPolicyRetriever interface

[ServiceContract]
    public interface IPolicyRetriever
    {
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();
        [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
        Stream GetFlashPolicy();
    }



Step2 : Implement IPolicyRetriever interface in your service

public class Service1 : IService1, IPolicyRetriever
    {
#region [ IPolicyRetriever Members ]
        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        public Stream GetSilverlightPolicy()
        {
            string result = @"<?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>";
            return StringToStream(result);
        }
        public Stream GetFlashPolicy()
        {
            string result = @"<?xml version=""1.0""?>
                            <!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
                            <cross-domain-policy>
                                <allow-access-from domain=""*"" />
                            </cross-domain-policy>";
            return StringToStream(result);
        }
        #endregion
}



Tim Heuer describes another way in this video :
http://www.silverlight.net/learn/videos/all/how-to-use-cross-domain-policy-files-with-silverlight/


You have to put the files in the right place. When I get to work, I''ll tell you where I put them for our stuff.


It needs to go in the ROOT of the domain. This is important as it is not the application root, but the root web.
Even if your app is at mySite.com/myapp, the policy file needs to be at mysite.com/clientaccesspolicy.xml.
Refer:
http://timheuer.com/blog/archive/2008/04/06/silverlight-cross-domain-policy-file-snippet-intellisense.aspx[^]
http://www.netomatix.com/post/2010/03/09/Silverlight-Cross-Domain-Web-Service-Access-Error.aspx[^]

So just check if you are able to access it here: ''http://www.mywebsite.com/clientaccesspolicy.xml'' or ''http://www.mywebsite.com/crossdomain.xml''


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

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