다음을 통해 공유


Web Services and Silverlight

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:

  1. 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.

    App Dialog

  2. 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. 

--Cheryl

Comments

  • Anonymous
    March 30, 2008
    Dan Wahlin's latest articles, Cheryl at Silverlight SDK on SL2, WS, and WCF, Sam Landstrom on using

  • Anonymous
    April 01, 2008
    The WebClient and HttpWebRequest classes can both be used for retrieving data from a Web service. WebClient

  • Anonymous
    April 01, 2008
    I'll be using this page to link to Silverlight 2 articles and posts (both ones I write as well ones by

  • Anonymous
    April 08, 2008
    Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,TutorialsandLinksPage 我将使用本页链接到有关Silve...

  • Anonymous
    July 21, 2008
    (以下内容全部整理自博客堂Scottgu博客中文版)Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,...

  • Anonymous
    January 05, 2009
    Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,TutorialsandLinksPage 我将使用本页链接到有关Silve...

  • Anonymous
    June 15, 2010
    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

  • Anonymous
    August 08, 2010
    I am really grateful to have the information from this blog.I liked the blog as it has been written,the information  i got from here.