WCF and interoperability – Consuming REST Services

With [tag].NET Framework[/tag] 3.5 came new version of [tag]Windows Communication Foundation[/tag] – [tag]WCF 3.5[/tag], bringing us very useful set of new features and one of them is out of the box support for REST style services. Now it is easy to create and consume REST services, so without further delay lets do some real world interoperability example – consuming [tag]eBay[/tag] [tag]REST[/tag] API.

There is simple WPF application in the attachment with all the code to do some simple search and presentation of the result: eBay Search Application Source Code

Before you start

First of all you have to sign up for eBay’s developers program and generate production keys – here’s quick start guide. Once you acquire keys you can use them wherever I put your-app-id.

Set up client endpoint configuration

To use REST style services you have to configure client to use webHttpBinding and set behavior to be webHttp. Service address is http://open.api.ebay.com so configuration would look like this:

<pre>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
 &lt;system.serviceModel&gt;
  &lt;client&gt;
   &lt;endpoint address="http://open.api.ebay.com"
             behaviorConfiguration="ebay"
             binding="webHttpBinding"
             contract="eBayService.IeBayShoppingService"
             name="eBayShoppingService" /&gt;
   &lt;/client&gt;
   &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
     &lt;behavior name="ebay"&gt;
      &lt;webHttp/&gt;
     &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
   &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
&lt;/configuration&gt;</pre>

Create client proxy

Since we are going to use http get for executing eBay queries this is how the operation contract should look like:

<pre>[OperationContract]
[WebGet(
  BodyStyle = WebMessageBodyStyle.Bare,
  ResponseFormat = WebMessageFormat.Xml,
  UriTemplate = "shopping?responseencoding=XML&amp;appid=<em>your_app_id</em>&amp;siteid=0&amp;" +
                "version=525&amp;callname=FindProducts&amp;" +
                "QueryKeywords={keywords}&amp;MaxEntries={maxEntries}&amp;" +
                "AvailableItemsOnly={onlyAvailable}&amp;PageNumber={pageNumber}"
)]
XmlElement FindProducts(string keywords, int maxEntries, bool onlyAvailable, int pageNumber);</pre>

First of all we are going to use http get so WebGet attribute is required, response is going to be XML so we set ResponseFormat accordingly and since response is not wrapped with infrastructure provided elements Bare body style should be used. UriTemplate is specific to the call, we here define how the call will be executed, actually how the call url is going to be formed. {keywords} will be replaced by method’s keywords parameter value, {maxEntries} by maxEntries parameter value and so on. This method will invoke eBay’s FindProducts method, you can find more details on the eBay REST developer centre.

Since the result is XML the result of the method is XmlElement (XmlDocument is not serializable). [tag]WCF[/tag] will even deserialize the result to an object if you have the right object definition. Achieving that is actually quite easy. First of all DataContractSerializer is out of question knowing the result XML (you have example calls in the documentation and you can fetch the result from browser). DataContractSerializer is too inflexible for this job but WCF can use XmlSerializer which is much more flexible. To get our classes we can use xsd.exe utility coming with [tag]Visual Studio[/tag] or type in code manually :) . So now our operation contract would look like this:

<pre>[XmlSerializerFormat]
[OperationContract]
[WebGet(
   BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Xml,
   UriTemplate = "shopping?responseencoding=XML&amp;appid=<em>your_app_id</em>&amp;siteid=0&amp;" +
                 "version=525&amp;callname=FindProducts&amp;" +
                 "QueryKeywords={keywords}&amp;MaxEntries={maxEntries}&amp;" +
                 "AvailableItemsOnly={onlyAvailable}&amp;PageNumber={pageNumber}"
)]
FindProductsResponse FindProducts_Object(string keywords, int maxEntries, bool onlyAvailable, int pageNumber);</pre>

The only difference here is in instructing WCF to use XmlSerializer by using XmlSerializerFormatAttribute attribute and changing the result type to the one generated by xsd utility.

At The End

Interoperability is arguably one of the most challenging IT areas, there are always some quirks involved. I encountered couple of them myself even with this simple application. Aaron Skonnard shows some of the challenges with eBay SOAP API here. I guess it is never straight forward when it comes to interoperability, at least WCF helps in one part of it.

Summary

So this was not too bad. WCF makes using RESTful services easy and in new version brings us even more out of the box interoperability support. You can find a list of new WCF 3.5 features here. WCF 3.5 comes with [tag].NET Framework 3.5[/tag], more about features that .NET Framework brings are summarized in this MSDN article.

  1. #1 by Mark Morgan on February 26, 2008 - 6:34 pm

    Mihailo

    Like your article! I am currently working on an interoperable B2B order processing web-service. However the client has not asked for REST support as of yet – if so then I will get back to you!

    Any contractor requirements in DeCare at the moment?

    All the best

    Mark

  2. #2 by Mihailo Lalevic on February 27, 2008 - 9:19 am

    Hi Mark,
    Thanks for the comment, nice to hear from you again.

    I will check about contracts and let you know.

    Cheers,
    Mihailo

  3. #3 by infocyde on August 21, 2008 - 11:33 am

    Thanks for sharing your code. I’m having a hard time finding good examples/tutorials that use WCF. Your REST based example really helps.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: