views:

2496

answers:

3

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 :)

+4  A: 

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.

Darrel Miller
And you sir, win the "most wrong" award. Creating DataContracts to represent the remote data, and letting the various .NET serializers handle the dirty work is a huge win
TheSoftwareJedi
Darrel Miller
@TheSoftwareJedi I challenge you to create a data contract that will successfully de-serialize the XML responses you get from the twitter API. If you post the solution I will remove my answer.
Darrel Miller
DataContracts are *very* limited in what they can do when processing xml. I second Darrel's opinion, HttpWebRequest is much easier than having to deal with the whole WCF stack, which is unable to deserialize a *lot* of xml content out there, be it mixed content, attributes or decentralized extensibility (namespaces).
serialseb
+4  A: 

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"

TheSoftwareJedi
last link is borked
Steve
@Steve, just edit it for me next time! This is a wiki...
TheSoftwareJedi
i do not have edit rights with my current SO points level.
Steve
A: 

check this

http://blog.flair-systems.com/2010/05/how-to-consume-rest-based-services.html

Waleed Mohamed
I have seen this link posted all over wcf questions and in most cases it doesn't really help. The simplest way to consume wcf is via the HttpClient extensions as part of the Rest Start Kit Preview 2. Why on earth would you do as in the above link?
Joshua Hayes