CommunicationException with Silverlight and a cross domain call

You are calling your services from Silverlight but the services are accessed via a different URI than the Silverlight application is being hosted. Errors galore, right? 

Silverlight honors the protection that helps mitigate a sites scripts from being called if someone simply copied your app to their server. 

The error you get is this: 

An error occurred while trying to make a request to URI ‘http://localhost.:2935/Services/UserService.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 cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details. 

This error is very easily fixed. 

Create a file in you web root. Must be the root called clientaccesspolicy.xml . It must be named exactly and must live in the root. 

File: clientaccesspolicy.xml

Now, copy this into the file: 

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

Save it, rebuild and run. Voila, you should now be up and running. If you need more info, check this helpful link. 

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *