Home > REST, Silverlight > REST Client Library for Silverlight

REST Client Library for Silverlight

February 8th, 2010

I’ve been doing some hackery and ported the client libraries of the WCF REST Starter Kit to Silverlight. The library greatly simplifies things when it comes to accessing and consuming RESTful services and resources in your Silverlight application.

Here’s a sample for a simple GET using the HttpClient class:

public User GetUser(string userId)
  //init client with base URI
  HttpClient client = new HttpClient(http://localhost:56789/users/);

  //send a HTTP GET request for a given resource
  HttpResponseMessage response = client.Get(userId);

  //parse the received data on the fly
  return response.Content.ReadAsDataContract<User>();


Or, in order to send a DTO via HTTP POST:

public void PostUser(User user)
  //init client with base URI
  HttpClient client = new HttpClient("http://localhost:56789/");

  //serialize user object
  HttpContent content = HttpContentExtensions.CreateDataContract(user);

  //post to user service
  HttpResponseMessage response = client.Post("/user", content);


I have removed a few bits, but most of the functionality should be there, and I added “NOTE” comments to all the sections that I had to modify. I haven’t used this port in-depth but managed to access my RESTful resources as expected so far, and I hope it will work just fine as in most scenarios that are covered in the original Starter Kit. Of course, given the Starter Kit itself is prerelease software, this applies to this release even more.


Blocking vs. Async Operations

If you try to initiate a blocking request to your REST service on the UI thread, an InvalidOperationException is thrown due to synchronization issues. Accordingly, a snippet as the one above should always be invoked on a worker thread (which is a best practice anyway).

However, in order to simplify things, I’ve added a bunch of XXXAsync extension methods for the HttpClient class that allow you to invoke HTTP operations such as GET, POST, PUT etc. directly on the UI thread, and have the result delivered back to you through a simple callback action. Here’s the same snippet as above, this time using the GetAsync method of the HttpClient class:

public void GetFolder()
  HttpClient client = new HttpClient("http://localhost:56789/root");

  //you can invoke the GetAsync method on a worker thread
  client.GetAsync(response =>
      VirtualFolder folder = response.Content.ReadAsDataContract<VirtualFolder>();


Note that the callback action will be invoked on a worker thread, so you might have to switch back to the UI thread if you want to modify your UI.


Oh yes: Use at your own risk 😉


Download REST Starter Kit for Silverlight

Current Version: 1.0.3, 2010.02.14

Author: Categories: REST, Silverlight Tags: ,
  1. February 10th, 2010 at 17:08 | #1

    Where is the source for the WCF REST Toolkit? Is it included in the download?

  2. February 10th, 2010 at 17:10 | #2

    Only the client bits that need to run under Silverlight are included. The full starter kit (which is not SL compatible) is available via CodePlex, of course.

  3. Bob
    February 15th, 2010 at 21:41 | #3

    Hey Philipp, thank you for posting this…do you have a sample app using this code by any chance? I have tried it in a couple different scenarios and I am running into some trouble.
    Thanks again,

  4. February 15th, 2010 at 22:40 | #4


    I’ll be releasing a few bits on VFS and a few tutorials around the subject that will also show off RESTful data access from Silverlight, but in the mean time, just have a look for Starter Kit tutorials (especialls the covering the HttpClient class). Those aren’t SL tutorials, but you should be able to use the code 1:1.


  5. Bob
    February 16th, 2010 at 02:24 | #5

    Thanks Phiipp, I look forward to it – but I think my problem is not necessarily related to the kit itself. I am trying to access a web service that is running on my local machine in IIS, but I get an HRESULT 0x80000012 HttpStageProcessingException. I was having a similar problem when I was using WebClient and trying access my service via http://localhost/… Moving my service to IIS and referencing it via machine name cleared it up then, but I am having no such luck this time and I am not sure if you have seen this before.
    Thanks again,

  6. March 20th, 2010 at 21:55 | #6

    I’m hitting some stupid issue using these bits. I’m somehow unable to declare an HttpContent. For example:

    var form = new HttpUrlEncodedForm();
    form.Add(“status”, “Test tweet using Microsoft.Http.HttpClient”);
    var content = HttpContent.Create(form);

    Gives the error:
    Error 1 Inconsistent accessibility: parameter type ‘Microsoft.Http.HttpContent.Content’ is less accessible than method ‘Microsoft.Http.HttpContent.HttpContent(Microsoft.Http.HttpContent.Content, string, long?)’ C:devIGCIGCRest Starter Kit for SLREST Client LibHttpContent.cs 23 16 Silverlight REST Client

  7. March 20th, 2010 at 22:09 | #7

    You’ve changed the constructor at line 23 in httpContent.cs to a public which causes the problem. I’ve set it to private and the issue has gone away, but I’m not using the Extensions, which this change might also affect.

  8. Paul Ashton
    May 11th, 2010 at 11:51 | #8

    Hi Philip, I have been trying to get my head around the project you supplied.

    My need is to async get and process the result.

    Any chance you could provide a code snippet?
    I have been unsuccessful when trying to use client.Get as per your example as the Get method is not there, so I am trying Microsoft.Http.AsyncHttpClientExtensions.GetAsync but the signature is defeating me 🙁

    Any help greatly appreciated

    I am using VB.net silverlight 4


  9. May 28th, 2010 at 08:13 | #9

    All the functionality is provided via extension methods (see the class HttpMethodExtensions). Make sure you have in import of this namespace in order to get all these methods.


  10. July 30th, 2010 at 20:13 | #10

    FYI, I’ve created a new library that makes connecting to RESTful POX or JSON based services from a Silverlight client much more like it is from full .Net/WCF (ala WebGet, WebInvoke, UrlTemplates). Check out my website, http://slrest.codeplex.com/ for details.

  11. cecilphllip
    August 10th, 2010 at 05:05 | #11

    just found this today
    “Create SendAsync Convenience Extensions for the WCF REST HttpClient to GetAsync and PostAsync”


  12. Leo E.
    September 9th, 2010 at 20:00 | #12


    I wonder what made you use hard-coded timeout in HttpWebRequestTransportStage?


  13. October 30th, 2010 at 09:38 | #13

    @Leo E.
    It’s a port of the MSFT REST toolkit. There’s a lot of atrocious code in there. I not too much into hard coding myself (despite the name of a particular blog) 😉

  14. December 7th, 2010 at 16:35 | #14

    Great Job Philipp!!!. This is exactly what I need for one project I am working on. Thanks

  15. Marco
    January 6th, 2011 at 23:30 | #15

    Hi Philipp,

    I wonder how I can catch time out exceptions using the SL port of HttpClient. Can you give me sample codes?


  16. Denis
    February 9th, 2011 at 18:30 | #16

    Hi, I was wondering too about the timeout. How to change them and how to catch exception.


  1. February 8th, 2010 at 19:26 | #1
  2. February 16th, 2010 at 00:22 | #2
  3. February 22nd, 2010 at 10:03 | #3