Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming a REST service from WCF

I'm not that familiar with WCF, but I thought I'll learn while trying to consume an existing service.

One of the REST APIs I thought of was the Twitter API. I thought of developing a WPF client that will just output to the screen the last 5 tweets by a certain Twitter user.

I was wondering if someone could please briefly outline the steps I need to take in Visual Studio to consume these services, using WCF (classes, wizards, proxies etc.).I already know how to just call them using a web request and parse the XML that returns - I really want to see the WCF part at work.

Thanks in advance to anyoine who helps further my education :)

like image 906
Traveling Tech Guy Avatar asked Nov 07 '08 17:11

Traveling Tech Guy


2 Answers

Check out Kirk Evans Creating a REST Twitter Client With WCF. The latest improvements to WCF in .NET 3.5 SP1 make many RESTful interfaces easier.

Also check out the Twitter WCF 3.5 API Declaration Library from the MSDN site.

Here's yet another example - WARNING as of 2/3/10 link is "borked"

like image 138
TheSoftwareJedi Avatar answered Sep 20 '22 03:09

TheSoftwareJedi


There is no benefit to using WCF to consume an Http based API like the Twitter API. System.Net.HttpWebRequest is more than sufficient. In fact I suspect that you will have some difficulty. WCF is much easier to get working when you have WCF at both ends of the wire.

However, if the REST API is returning Atom content then you could using the System.ServiceModel.Syndication classes to help parse the response.

EDIT: Since I wrote this post Microsoft released a preview of a new HTTP client library that does an even better job of consuming RESTful services.
Here is how you would use it to POST to twitter:

var client = new HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic("username","password");
var form = new HttpUrlEncodedForm();
form.Add("status","Test tweet using Microsoft.Http.HttpClient");
var content = HttpContent.Create(form);
var resp = client.Post("http://www.twitter.com/statuses/update.xml", content);

If you want more more details on this client library, I am in the process of writing some blog posts about it here.

like image 25
Darrel Miller Avatar answered Sep 21 '22 03:09

Darrel Miller