Wednesday 6 February 2013

Silverlight: Cross domain WCF service calls

Question:

How to access a WCF service on domain A from a Silverlight application on domain B?
I have a silverlight app on localhost:8008 and a WCF service on localhost:8080. My silverlight app cannot access the WCF service. What can I do?

Answer:

When you call a WCF service on a different domain you might run into a security issue. Since Silverlight runs inside a browser you are not allowed to access web-services on another domain for security reasons.

Your WCF service must explicitly define a client access policy and a cross domain policy.

To do so, you simply add two xml files to the root of your WCF service.
The first is : clientaccesspolicy.xml
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*">
        </domain>
      </allow-from>
      <grant-to>
        <resource include-subpaths="true" path="/">
        </resource>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>



The second is : crossdomain.xml
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="*">
  </allow-http-request-headers-from>
</cross-domain-policy>

No comments: