SilverLight跨域访问及其常用的几种解决方法

 

1、文章出处:http://www.cnblogs.com/liaohenchen/articles/silverlight-cross-domain.html

今天在做silverlight访问JSon数据的时候老是出现错误,才发现是跨域的问题,因此将这方面的内容整理一些,列了出来

SilverLight 出于对安全性的考虑默认情况下对URL的访问进行了严格的限制,只允许访问同一子域下的URL资源。

下表列出了Silverlight 2.0 中 URL 访问规则:

 
SilverLight跨域访问及其常用的几种解决方法 - fly - fly
 

如果WCF与SilverLight Web不是在同一站点,那么我们就要在被访问端的根域放上两个XML文件clientaccesspolicy.xml,crossdomain.xml 如果要通过WebClinet访问另一站点的资源,那么需要在被访问站点放上域访问策略xml文件,不然在Complete事件里面的事件参数报告空对象引用。

clientaccesspolicy.xml文件格式如何,切忌最好要将编码设置为utf-8,否则极易出错

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

crossdomain.xml文件格式

crossdomain.xml的格式非常简单,其根节点为<cross-domain-policy> ,其下包含一个或多个<allow-access-from>节点,<allow-access-from>有一个属性domain,其值为允许访问的域,可以是确切的 IP 地址、一个确切的域或一个通配符域(任何域)。下边是两个例子:

程序代码 <?xml version="1.0"?> <cross-domain-policy>   <allow-access-from domain="http://www.lishewen.com.cn/" />   <allow-access-from domain="*.lishewen.com.cn" />   <allow-access-from domain="222.217.221.16" /> </cross-domain-policy>

程序代码 <?xml version="1.0"?> <cross-domain-policy>   <allow-access-from domain="*" /> </cross-domain-policy>

第二个例子允许任何域的访问。对于crossdomain.xml文件存放位置,建议将其存放于站点根目录中!

如:http://bbs.lishewen.com.cn/crossdomain.xml

在这我也弄了个Silverlight的例子来测试

http://silverlight.lishewen.net.cn/SyndicationFeedReader/

关于clientaccesspolicy.xml,crossdomain.xml的具体说明,请大家参看MSDN  http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

文章整理资料来源于:http://www.cnblogs.com/format/articles/1282203.html

               http://blog.lishewen.com.cn/post/2008/06/e4bdbfe794a8crossdomainxmle8aea9Silverlighte58fafe4bba5e8b7a8e59f9fe4bca0e8be93e695b0e68dae.aspx

2、文章出处:http://www.cnblogs.com/akiing/archive/2009/11/17/1604489.html

WCF跨域 这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 尝试向 URI“http://localhost:8001/AccountService.svc”发出请求时出错。这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 服务。您可能需要与该服务的所有者联系,以发布跨域策略文件并确保该文件允许发送 SOAP 相关的 HTTP 标头。出现此错误也可能是由于使用的是 Web 服务代理中的内部类型而没有使用 InternalsVisibleToAttribute 属性。有关详细信息,请参阅内部异常。

解决方法:

  一、在WCF项目根目录下添加clientaccesspolicy.xml文件

Code <?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> 二、在silverlight项目中添加一个中介类ServerManager.cs

Code public class ServerManager     {         private static ServiceWcfClient servicePicture = new ServiceWcfClient();

        internal static ServiceWcfClient GetPox()         {             if (servicePicture.State == System.ServiceModel.CommunicationState.Created)             {                 servicePicture.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:52207/ServiceWcf.svc");                 return servicePicture;             }             else             {                 return servicePicture;             }         }     } 三、实例化实体类的时候一般是这样:ServiceWcfClient clientWcf = new ServiceWcfClient();

换成:ServiceWcfClient clientWcf = ServerManager.GetPox();

原文地址:https://www.cnblogs.com/zhangxin1989/p/2667659.html