I've been watching the Silverlight forums since the release of Silverlight 2, and I've noticed folks are having problems getting their Silverlight 2-based apps to work with Web services. There have been a number of API changes between Silverlight 1.2 and 2, including the removal of the BrowserHttpWebRequest class and related APIs and the addition of HttpWebRequest, HttpWebResponse and supporting classes. Silverlight 2 offers built-in support for asynchrous Web services calls only. In addition, the Web services proxy support has changed from ASP.NET style proxies to WCF proxies. On top of this, cross-domain issues are tripping up some folks. Silverlight-based apps can only call services hosted in the same domain, or services that have a cross-domain permission in place.
I've got a couple of tips to offer and I can point you to some great blog posts on the subject. First the tips:
- When you create your Silverlight project to call an existing Web service in the "Add Silverlight Application" dialog, make sure you choose the default option that adds a new Web project to test your Silverlight-based application. You'll get a security exception if you try to call a Web service using a dynamically created HTML test page.
- If you are creating the WCF service and the Silverlight-based app to call the Web service and you plan to host them from the same domain, follow the steps listed in the Quickstart that demonstrates this. If you follow these instructions and want to debug your app from Visual Studio, the Silverlight-based app and the Web service will be hosted from the same port in the ASP.NET development server (same host, different ports == different domain). You should also be able to deploy them to IIS without any cross-domain problems. Here is a summary if you don't want read the Quickstart:
The first step is to create a WCF service. In the New Project Dialog in Visual Studio, select "Web", then "WCF Service Application". Once you've added the desired functionality to your service, test it using a browser.
- You'll need to manually change the endpoint binding of the service to "basicHttpBinding" to interop with Silverlight. See the Quickstart for more details on how to do this. Once everything works to your satisfaction, add the Silverlight client. I'll repeat myself...update the binding before you add the Silverlight client.
To add the Silverlight client: In Solution Explorer, right click the solution and select "Add New Project", select "Silverlight Application". In the Silverlight Application dialog, choose "Link this Silverlight control into an existing Web site". Link it to your WCF service.
- To add a service reference, right click the Silverlight application in Solution Explorer, and choose "Add Service Reference".
Use the generated proxy (created when you added the service reference) to make calls from the Silverlight-based app to the Web service. Since you've already changed the binding to basicHttpBinding, everything should work just fine. If things don't work fine, double check the binding. If you didn't change it to basicHttpBinding, do so now and update the service reference.
Finally, there are some great blog posts that go into more detail about some of the items I've touched on. Check out Tim Heuer's post that discusses how to call ASP.NET, WCF and REST services from Silverlight and Pete Brown's post, which delves into WCF and Silverlight integration; including an explanation of the service binding requirements and more details about how to allow cross-domain calls from your service.