Thursday, July 02, 2009

Silverlight 2.0 – Silverlight Apps calling asmx web services

A while back I worked on a Silverlight application that was hosted in SharePoint. The Silverlight application was to call a web service written by a third party vendor in .NET 2.0.

As what we all normally would do in a Silverlight app is to call this web service. However, I got the following error when my application tries to call the asmx web service:

“An error occurred while trying to make a request to URI 'http://localhost:1000/webservice.asmx'. 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. Please see the inner exception for more details.”



After some research, I found a recipe to get it going again:

  1. Create a new file within the web service application (at the root of the requesting domain), called clientaccesspolicy.xml with the following contents:

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

  2. Create another file within the web service application(at the root of the requesting domain), called crossdomain.xml with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <allow-http-request-headers-from domain="*" headers="*"/>

After adding these policy files, my app started working. Hope this helps.



Hoài Nam said...

I got the same error.
Can you tell me exactly where to put the 2 files clientaccesspolicy.xml and cross-domain.xml?(i mean the folder on my PC).

Thank you.

Douglas Leung said...

Hey Hoai,

These files need to sit where your web services are hosted, in the root with web.config.


Reinhart said...

You're a life saver !

Searched for 3 hours using the MSDN explanation, and they omitted the header directive !

Thanks !