Consuming REST Services from Silverlight

Tags: REST, Google, API, Silverlight

Silverlight has a relatively advanced network stack which allows you to perform GET and POST requests to both the same domain or other domains, provided they expose a cross-domain policy (crossdomain.xml or clientaccesspolicy.xml). With Silverlight 3 it is possible to perform other request types, such as PUT and UPDATE.

In Silverlight 2 the HTTP stack used the browser HTTP stack, but in Silverlight 3 you can opt in to the client HTTP stack. To create a HTTP request using the client HTTP stack you must use the WebRequestCreator, like so:


HttpWebRequest req = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(
new Uri(""));

This allows you to access REST services directly from the Silverlight application, making it easy to create mashup type applications. In this video Google engineer talks about the Google AJAX Search API being accessed from Silverlight. I hadn't found any implementations online, so I added it to the Reimers.Silverlight assembly. It supports web search, news search, image search, video search, local search and blog search.

The Reimers.Silverlight library currently includes support for two REST based services, Google's AJAX Search API and Panoramio's image search. The REST services are integrated into the static Services class, and can be accessed by passing the search parameters and a callback delegate to be invoked when the response is returned. The following shows an example request


private void ProcessWebSearch(SearchResult<WebSearchResult> results)
foreach (WebSearchResult item in results.Data.Results)
// Process search result
Reimers.Silverlight.Services.WebSearch("silverlight", "your Google Key", null, ProcessWebSearch);

Remember to display proper credits when you display results from a REST service that is not your own.

Latest Tweets