Wednesday, March 19, 2008

Silverlight Cross-Domain Calls

I have been playing around with the latest Silverlight 2.0 Beta. Specifically, working through the excellent tutorial on Scott Guthrie's blog. (By the way, Matt Berseth has provided a working example of this tutorial available here.) So, after doing a few sections of this, I decided that I wanted to be able to pull some of my own data, instead of using the Digg Service. So I wrote a small REST service using WCF with.Net 3.5. I was able to quickly get this up and running and then create a new Silverlight app to consume this service. However, when I would try to connect to the REST service using my Silverlight app, I was always getting an "Download Falure" error.

I did some digging into this and found that in order for a Silverlight (or Flash) app coming from one domain to consume data from services on another domain, there must be a policy mechanism on the service domain that grants access to the domain of the application. This policy must be available from the root of the domain. Unfortunately, http://localhost:8001/ and http://localhost:8002/ are considered different domains. So in order for my Silverlight app on port 8001 to talk to the REST service on port 8002, I needed to provide this policy access. I found this post by Carlos Figueria that covers this scenario and how to setup your service to host the policy files for Silverlight and Flash when your service is self-hosted and in a web site.

No comments: