Adventures in Software Craftsmanship
XAML, ASP.NET MVC, HTML 5, CSS, JavaScriptConsuming 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);
}
});
});
January 23, 2011 8:59 pm
[...] 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 [...]
January 23, 2011 9:34 pm
One word….AWESOME!
January 24, 2011 8:33 am
Joe, just as an FYI the release zip was updated for preview 3 to contain the IQueryable fix when that commit was made.
January 24, 2011 10:13 am
Good to know, thanks Glenn!
March 6, 2011 5:39 pm
[...] 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. [...]
May 10, 2011 5:59 pm
[...] 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/ [...]
June 17, 2011 3:42 pm
Terrific go through as I was uncertain what direction to go and just found my remedy
December 11, 2011 12:40 pm
[...] 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 [...]
January 23, 2012 3:57 am
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}”
February 29, 2012 3:21 am
How can I download the code ? It seems I cannot find a download link on the link provided in this blogpost?
March 28, 2012 10:15 am
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