How to consume REST services with WCF

As you are probably aware by now, Windows Communication Foundation (WCF) 3.5 introduced a new binding called WebHttpBinding to create and to consume REST based services. If you are new to the WCF Web Programming model then see here for more details.

There have been many articles and blogs on how to host a RESTful service. However there doesn’t seem to be much written work on how to consume these services so I thought to write a few lines on this topic.

The new WebHttpBinding is used to configure endpoints that are exposed through HTTP requests instead of SOAP messages. So you can simply call into a service by using a URI. The URI usually includes segments that are converted into parameters for the service operation.

So the client of a service of this type requires 2 abilities: (1) Send an HTTP request, (2) Parse the response. The default response message format supported out of the box with the WebHttpBinding is “Plain old XML” (POX). It also supports JSON and raw binary data using the WebMessageEncodingBindingElement.

One way of consuming these services is by manually creating a HTTP request. The following example is consuming the ListInteresting operation from Flickr:

-Do the HTTP request and include all the parameters as part of the URI

-Get the response that is in XML format

-Either parse it or deserialize it into an object

The above code works but it is not elegant: We are not using the unified programming model offered by WCF and the URL is hacked together using string concatenation. The response is also manually deserialized into an object. With WCF and the WebHttpBinding we can automate most of this.

Hi Pedram! Thanks for the info. There’s a bug though – you have an endpointBehavior configured, but your endpoint is not referring to it. You need to add ‘behaviorConfiguration="flickr"’ to the endpoint. This is something I keep forgetting to do myself!

when i call wcf method from extjs cross domain ,the response format can not adapt ,because it is either json or xml,neither is <script></script> format,it is perfect if wcf can response a cleartext format!

Hello! I’ve read you article lots of times, but I don’t understand any of those app.config settings. I really would like to use REST services from Community Server. I’ve got the documented REST API, but I need a little help. Please check it out:

Honestly, code wise, this is one of the best examples I've seen because you are, indeed, consuming an outside service and not trying to build it inside the app. That being said, what is missing from this example is several key screen shots on WHERE you are sticking your code inside the MVC api. This is not intuitive to beginners like me, and anyone familiar with the Visual Studio ASP.NET MVC environment knows there are multiple areas in the structure in which code is nested. This is why, as straightforward as your code seems, it is still not very helpful, and yet it really is one of the best examples I have found.

I agree with Jessica. There are several missing pieces to make this work. First of all how would the client application know that is the ServiceContract to be used in the Channel factory. I mean, since for the webHTTP binding SOAP header is not available.? Do i need to make direct reference? Even if so, after trying I dont see the channelfactory able to recognize my webhttp service hosted elsewhere. Request you to put screenshots of the all the pieces of code, if possible a sample app.