I am working on a small Silverlight project where I have to make a web service call to a remote server and display the returning data. I don’t have any control on the remote server, and unfortunately the remote server doesn’t have the necessary cross domain access policy file. In case you don’t know what this is file(s) is; Silverlight can make calls back to the domain that hosts the Silverlight control. However when you want to make a web service call to other domains such as Facebook, Twitter, Amazon, Flickr etc; you need have a special permission file in those servers: Clientaccesspolicy.xml or Crossdomain.xml. This file must be at the root of the remote server (Note: If the clientaccesspolicy.xml is not in the root, you will get a security access exception). Not all the time you can ask somebody to put the file on their servers, so then what?
You may find different solutions, including trying to get the cross domain access policy file to be put on to the server; but if you can’t, maybe you can use proxy design pattern. In this design pattern, you create a proxy object, and your application communicates with this proxy object, and your proxy object communicates with the actual object. Basically your proxy is the messenger in between; however you can put business logic here in the proxy, that you possibly can’t in the actual object.
In my scenario what I decided to do is, create a WCF service on the host the Silverlight app is running, create the clientaccesspolicy.xml on this domain, so the Silverlight app can access the WCF service. WCF is acting like a proxy here; and it is calling the actual web service on the remote server. After I created the WCF project, and created the clientaccesspolicy.xml file, I ran the app, and got the security access exception error!!. Spending some google time, and trying different variations of unlimited number of solution offers, my problem is solved with 3 things.
- When you out the clientaccesspolicy.xml file on the WCF project, change its copy to output directory option property to Copy always
- If this is a WCF service, make sure in the clientaccesspolicy.xml file, you have:
in stead of :
- Silverlight supports basic binding, whereas default WCF binding is wsHttpBinding; so go to web.config on the WCF service project and change the binding from wsHttpBinding to basicHttpBinding.