JM

Adventures in Software Craftsmanship

iOS, JavaScript, XAML, HTML5, CSS

Consuming WCF Web (REST) Apis in Silverlight

UPDATE 1/8/2013: Code now on github: http://github.com/joemcbride/HttpContrib

Microsoft, including the MEF Master Glenn Block, is hard at work trying to provide an easier way to create RESTful web services.  You can currently see the progress of their work on Codeplex, http://wcf.codeplex.com/ Unfortunately they currently do not support Silverlight or Windows Phone 7 clients to access the services in a friendly manor.

To that end, I created a ‘SimpleHttpClient’ which can be used in Silverlight to access a WCF Web Api.  It currently can be found as a fork of the WCF HTTP Contrib project.  Silverlight does not provide the ability to create your own custom query provider, so this ‘SimpleHttpClient’ and classes do not use LINQ directly, though the API is fairly similar (using extension methods).  The client & helper libraries were written from scratch with no direct dependencies on the core http framework.  I’m currently also working on a WP7 compatible version.  If you’re interested in seeing improvements to this or have any suggestions I’d love to hear them.  Hopefully this helps in your quest to write and consume REST Apis in Silverlight.

EDIT: Vote up Silverlight support for WCF REST at the codeplex site! http://joem.me/fnKy5Q

EDIT2: Note that for this to fully work with Preview 3, make sure you have this patch

http://wcf.codeplex.com/Thread/View.aspx?ThreadId=242523

And the UriTemplateOperationSelector changes from this changeset:

http://wcf.codeplex.com/SourceControl/changeset/changes/c4e21202c82d

Get Source: https://hg01.codeplex.com/forks/xamlcoder/wcfhttpcontrib

Here are a few sample queries:

// Get all people
SimpleHttpClient client = new SimpleHttpClient("http://localhost:1182/people");
// can also use MediaType.Json, which is the default
client.Accept = MediaType.Xml;

var query = client.CreateQuery<Person>();

HandleQuery(query);
// Get person by ID
int id;

if (Int32.TryParse(this.uxPersonID.Text, out id))
{
    SimpleHttpClient client = new SimpleHttpClient("http://localhost:1182/people");

    var query = client.CreateQuery<Person>().Where(c => c.ID, id);

    HandleQuery(query);
}
// Get top 3 people
SimpleHttpClient client = new SimpleHttpClient("http://localhost:1182/people");

var query = client.CreateQuery<Person>().Take(3);

HandleQuery(query);
// Get 3rd person
SimpleHttpClient client = new SimpleHttpClient("http://localhost:1182/people");

var query = client.CreateQuery<Person>().Skip(2).Take(1);

HandleQuery(query);
// Handle query method used above
private void HandleQuery(HttpQuery<Person> query)
{
    var task = query.ExecuteAsync();
    task.ContinueWith(t =>
    {
        Execute.OnUIThread(() =>
        {
            if (!t.IsFaulted && t.IsCompleted && t.Result != null)
            {
                t.Result.Apply(p => { Debug.WriteLine("Person: {0}", p); });
            }
        });
    });
}
// Create new person
Uri uri = new Uri("http://localhost:1182/people");

SimpleHttpClient client = new SimpleHttpClient(uri.ToString());

var contact = new Person { ID = 5, Name = personName.Text };

var stream = contact.WriteObjectAsXml();

var request = new HttpRequestMessage(HttpMethod.Post);
request.Accept = MediaType.Xml;
request.ContentType = MediaType.Xml;
request.RequestUri = uri;
request.Content = stream;

var task = client.SendAsync(request);
task.ContinueWith(t =>
{
    Execute.OnUIThread(() =>
    {
        var person = t.Result.ReadXmlAsObject<Person>();

        if (person != null)
        {
            Debug.WriteLine("Person: {0}", person);
        }
    });
});

11 Comments

  1. [...] This post was mentioned on Twitter by techno@solidsoft, Larry King. Larry King said: Consuming WCF Web (REST) Apis in Silverlight « joe.mcbride http://bit.ly/fopmZB #SL #RIA [...]

  2. One word….AWESOME!

  3. Joe, just as an FYI the release zip was updated for preview 3 to contain the IQueryable fix when that commit was made.

  4. Good to know, thanks Glenn!

  5. [...] Consuming WCF Web (REST) Apis in Silverlight – Joe Mcbride blogs about a new SimpleHttpClient he created for accessing WCF Web API services and other services. [...]

  6. [...] Joe McBride wrote such a wrapper class and blogged about it here: http://xamlcoder.com/blog/2011/01/23/consuming-wcf-web-rest-apis-in-silverlight/ [...]

  7. Terrific go through as I was uncertain what direction to go and just found my remedy :-)

  8. [...] API that Glenn Block and team are working on. I really love what they have done and I found a blog post by Joe McBride that provided exactly what I needed to use it in Silverlight as [...]

  9. Hi there. Great post.
    Is there a way to pass parameters in UriTemplate in WCF Web Api in this format :
    “http://localhost:1182/people?skip={skip}&take={take}”

  10. How can I download the code ? It seems I cannot find a download link on the link provided in this blogpost?

  11. Hey Josh, the code can be downloaded using tortoise HG (a Mercurial source control client) via this link:

    https://hg01.codeplex.com/forks/xamlcoder/wcfhttpcontrib

RSS feed for comments on this post.

Leave a Response

Latest from Twitter