2010-05-17 105 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