开发者

Cross Domain Policy

I am working with a Silvelright App that consumes a WCF service, I have placed a crossdomain and clientaccesspolicy xml's in the wwwroot of the IIS as well as in the application folder!

yet when the client communicates with the service, it throws an error saying;

An error occurred while trying to make a request to URI ‘http://localhost:1528/MyService.svc’. 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 ……

Please help! Thanks


The clientaccesspolicy.xml needs to be on the same port as your service. It needs to be located at http://localhost:1528/clientaccesspolicy.xml

If you are self hosting the WCF service then you need to host the clientaccesspolicy.xml from within your WCF service. The easiest way I've found to do this is to add a separate service contract that provides an HTTP GET of the clientaccesspolicy.xml.

[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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜