.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Consuming External WebService from Silverlight Application

Posted By:Manas Patnaik       Posted Date: February 10, 2011    Points: 75    Category: Silverlight    URL: http://www.dotnetspark.com  

Accessing web services from Silverlight.

While exploring Map api integration with SOI application, i felt that a separate post on consuming external web services from SL application worth a write.As we know services can be SOAP based (WCF bases)or can be REST (Simple HTTP ) based on the implementation technology.As you goggle you will find a lots of services from different providers , here (webservice directory) .May be some are useful saving lots of time some may look fancy.

Silverlight can access some services directly for e.g HTTP/REST type and some using proxy for SOAP .Well before  describing the process lets have a look at web service in Silverlight.Services hosted by various host comes with various security policies .For e.g yahoo allows access to the services to authenticated request only and authentication can be achieved using OpenID,Oauth and Hybrid (Yahoo API Auth.) , similarly Google services .These external service administrator  creates policy files to restrict the request from other domain with out proper authentication.

Basics of Webservice

The article expects  readers will have a basic idea about webservices , i will suggest to refer the tutorials from W3C to refresh the concept (W3C Webservices concept).

Cross Domain Policy for services

What is Cross Domain Access

Suppose Domain A is hosting a webservice .A request made from Domain B to the service at A is said to be Cross Domain access.

By default the Silverlight allows access to the services hosted at same domain with out any restriction but if its external then that service should allow your domain specifically.Generally the service providers uses crossDomain policy files to grant/restrict the request.On the same line if you are going to build your own service and want to be accessed from other domain then you have to add clientaccesspolicy.xml ,where either you can allow all external requests or else to particular domains.

The typical structure of a clientaccesspolicy.xml can be as bellow, here it allows access to all-

<;?xml version="1.0" encoding="utf-8"?>




      <;allow-from http-request-headers="*">

        <;domain uri="*"/>



        <;resource path="*" include-subpaths="true"/>





As mentioned above some of the services such as for images,news feed are open for all , such as from Geonames .You can find more details about these apis here.

External RESTful Service from Silverlight

As we know Silverlight client sitting inside a browser has restricted access to server ,For accessing database too it depends on proxy service calls through WCF.All the service calls from Silverlight is asynchronous by their behaviour , as contrary to synchronous call of ASP.Net. Well choosing Rest services here for this article can be seen as to maintain the simplicity and as most of the providers support it.Rest services exposes them selves through HTTP , using their url .On request made to the service it can return results in various format for e.g XML , JSON etc.

A external service from Silverlight can be called by 2 ways

  • Either ask the service provider/administrator to allow your domain to access the service ,by adding your domain to their policy files
  • Using proxy service call from your domain to that external webservice and your Silverlight app request to your domain proxy .

Now ,For sake of  example i have choosed a service from GeoName which allows me to get a place geographic detail as result.As mentioned their web .This a free service and allows all domain access.Their service url says ,

Url    »    api.geonames.org/postalCodeSearch?&username=***

Result    »    returns a list of postal codes and places for the placename/postalcode query as xml document

With your username and the search argument the url will be -:http://api.geonames.org/search?q=orissa&username=********

Now from your Silverlight code we can call the service using WebClient or HTTPWebRequest , The code for accessing a webservice can be

  1. string path = @"http://api.geonames.org/search?q=orissa&username=********";
  2. public MainPage()
  3. {
  4. InitializeComponent();
  5. }
  6. private void btnCallService_Click(object sender, RoutedEventArgs e)
  7. {
  8. WebClient client = new WebClient();
  9. client.DownloadStringCompleted += (s, ev) =>
  10. {
  11. MessageBox.Show(ev.Result);
  12. };
  13. client.DownloadStringAsync(new Uri(path));
  14. }
  • Create a Webclient for sending a HTTP request to server.
  1. WebClient client = new WebClient();
  • Assign the callback with the async operation completed event handler.
  1. client.DownloadStringCompleted += (s, ev) =>
  • Call DownloadStringAsync with service URL
  1. client.DownloadStringAsync(new Uri(path));

Here in above example we showing the result in a message box after calling service , instead of which we can parse the xml and get meaning full data source for our app.


The XML data can be parsed either using  XmlReader , Linq To XMl or using XmlSerializer which converts the xml data to plain clr objects.The parsing of XML will not be using XMLReader .I will cover the conversion of XML data to CLR object with example in my next post.


Not most of the services allow as simple access as mentioned above .A yahoo api invites some extra steps and some more code stuff.You may refer to this article for accessing yahoo web service from ASP.Net. Use of  3rd party proxy service also provides another way , for e.g 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend