While working with Silverlight applications, calling a WCF Service from Silverlight client, you might have faced cross domain issues. I would like to demonstrate the problem, root cause and solution for the issue. I have created a WCF Service a separate project and tried to consume the service in Silverlight project. To learn how to consume WCF Service in Silverlight project please refer to this blog post. All the service calls from the Silverlight client are asynchronous.
I have Referred the project using add service reference, and written the code to call the service methods asynchronously in Silverlight client. Able to build successfully but when I ran the application I got the following error on Debug.
The error details are below:
An error occurred while trying to make a request to URI 'http://localhost:5050/Labcalls/ AudioRecorder.Web /Audiorecorder.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. Please see the inner exception for more detail.
The WCF service and the website hosting the Silverlight app have a different port number! So this is called cross domain, issue since they have different port number silverlight considers it as different domain. So the silverlight application is expecting the website to use the same port number that service uses. It is not accepting the urls from other domains. This is due to Security restrictions in silverlight
Work Around ?
To fix this we need to have same port number for both Web Application and WCF Application. So Create WCF Service in the same web application project.
In other words if we need to have the service and client on the same domain in Visual Studio, we need to add WCF Service to the same Web Application project.
This can be done by right-click the Web Application that gets created when you create a Silverilght application, - >Add new Item-> Select Silverlight enabled WCF Service. This will ensure automatically that service and the application stay on the same domain.
Otherwise we need to give the port number of web application and service.
I have my service in different domain from application, how will I solve this issue?
The solution is simple. We need to have ClientAccessPolicy.xml and Crossdomain.xml in the root folder of the website.
Important thing is we need to have these files under the root folder of the website to make the service accessible across cross domain
You should be able to access these files
<?xml version="1.0" encoding="utf-8"?>
<resource path="/" include-subpaths="true"/>
This helps in granting access across the board with no restrictions.
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<allow-http-request-headers-from domain="*" headers="*"/>
Then your issue resolved, now you have access to the WCF Service and they are getting called with out any problems.