2010-05-17 126 views
0

我正在使用一个使用WCF服务的Silvelright应用程序,我在IIS的wwwroot以及应用程序文件夹中放置了crossdomain和clientaccesspolicy xml!跨域策略

当客户端与服务通信时,它会抛出一个错误说:

尝试向URI'http://localhost:1528/MyService.svc'发出请求时发生错误。这可能是由于尝试以跨域方式访问服务而没有适当的跨域策略或者不适合SOAP服务的策略。您可能需要联系该服务的所有者以发布......

请帮忙! 谢谢

+0

您能否更具体地了解WCF服务目前的发布情况。 – Johannes 2010-05-17 15:44:52

+0

嗨,http://bit.ly/aPzq68这为我工作! – Jayesh 2010-05-17 16:37:04

回答

0

clientaccesspolicy.xml需要位于与您的服务相同的端口上。它需要位于http://localhost:1528/clientaccesspolicy.xml

如果您自己承载WCF服务,那么您需要托管您的WCF服务中的clientaccesspolicy.xml。我发现要做到这一点的最简单方法是添加一个单独的服务合约,它提供clientaccesspolicy.xml的HTTP GET。

[ServiceContract()] 
public class PolicyRetriever 
{ 
    [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")] 
    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>"; 

     if (System.ServiceModel.Web.WebOperationContext.Current != null) 
     { 
      System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml"; 
     } 

     return new MemoryStream(Encoding.UTF8.GetBytes(result)); 
    } 
} 
+0

它帮助!我从http://bit.ly/aPzq68获得了更多信息[与urs不同] – Jayesh 2010-05-17 16:37:37