Don’t Create REST APIs with WCF REST Services…use Web API Instead

There was time when developers used to create Web Service using the WCF. Their argument was using basicHttpBinding makes a WCF service as a Web Service. Yes that ASMX based web service. I never agree to this argument.

image

Clearly WCF was an evolvement over ASMX Web Service. Now let us talk about purpose of this article, that saga of Web API and REST Service. To start with let us understand the background.

REST SERVICE

image

A typical REST Service in WCF would look like as follows:

clip_image002

Point to be noticed here is that while creating the service we are defining the resource type. Since in above service we want to work with both JSON and XML message format, we are creating two services for the same.

EndPoint for REST Service can be created using the factor as shown in the snippet below,

clip_image004

This is the simplest WCF REST Service one could create. We can have different HTTP operations on the same URL. We can perform HTTP GET, POST, PUT, DELETE on the same resource URL in the WCF REST service. However while creating the service, we need to take care of the message format. WCF REST service use the WCF channel and messaging architecture which is used in the WCF SOAP service.

ASP.NET WEB API

This is based on the ASP.NET MVC architecture to create an API for the web. We can create a RESTful API using the ASP.NET Web API. A simple Web API is a class which inherits ApiController class.

clip_image006

Keep in mind that ASP.NET Web APIs does not have the EndPoints and bindings. It utilizes the ASP.NET MVC routings to resolve the resources. Here resource describes their own capabilities and interactions.

To understand difference between REST Service and ASP.NET Web API, let us try to understand The Richardson Maturity Model.

RMM: Richardson Maturity Model classify an API into different levels on the basis of how well they use or take advantage of the web.

image

There are four levels in the RMM from level 0 to level 3.

image

Now we know different levels of RMM, so let us try to map WCF REST Service and the ASP.NET Web API to RMM.

In WCF REST Service

  • Can have multiple URI
  • Can have multiple HTTP methods
  • Operations are defined by the URI
  • Each operation has its own URI
  • Each URI cross ponds to a method on the server.

Let us examine nature of REST Service. Let us assume we want to perform CRUD operation on the BloodDonor table. WCF REST Service allows us to have different methods on the same URI. So it is very much resource oriented. Resource is uniquely identified by the URI and we can have multiple HTTP method on the same resource or URI. A WCF REST Service for CRUD operation may look like as shown in the listing below,

image

Before we conclude that WCF REST Service can be placed in the level 2, there is a catch in it. Above created WCF REST does not know the content negotiation. Client cannot request for a particular type of content and can only work with the XML message format (in this case). WCF REST service cannot negotiate with the resource on the same URI in the multiple message format.

image

Now let us focus on the ASP.NET Web API. It can be also be placed in the level 2 with content negotiation and the support for multiple message format on the same URI.

image

A typical ASP.NET Web API may look as shown in the listing below,

clip_image002[6]

ASP.NET Web API have single URI for multiple operations working on different HTTP methods. However unlikely WCF REST, while accessing ASP.NET Web API, client can negotiate the content. Client can pass the message formant want to work with.

Let us see this in action using the Fiddler. In the fiddler when we pass content type as XML, ASP.NET Web API will return response in XML message format as shown in the image below,

clip_image004[6]

In the fiddler when we pass content type as JSON, ASP.NET Web API will return response in JSON message format as shown in the image below,

clip_image006[6]

Clearly in the ASP.NET Web API client can pass the message format in the request header. Some other characteristics of the ASP.NET Web API over the WCF REST Service is as follows:

  • Unit Testability
  • Support for multiple format
  • Symmetric programming experience at client and server
  • Multiple hosting options

By discussion so far we can say you don’t create REST Service using the Web API. Web API is has more features to put itself in the level 2 of the Richardson Maturity Model.

image I am sure this is a subjective discussion, so would love to read your thought on the same. Feel free to comment your view on my understanding.

Happy coding.

7 responses to “Don’t Create REST APIs with WCF REST Services…use Web API Instead”

  1. […] You do not create WCF REST Service using ASP.NET Web API (Dhananjay Kumar) […]

  2. Isn’t it possible to do content negotiation in WCF REST service by setting the “automaticFormatSelectionEnabled” to true, as described in the documentation?

    https://msdn.microsoft.com/en-us/library/ee476510.aspx

  3. Two Questions here:
    1) Why is ‘api’ there in the url of Web API ? why can’t we have normal URL like any other MVC controller action.
    2) Please pardon my lack of knowledge but who is ‘Richardson’ ?

  4. As per my previous comment (which I can no-longer see) – Can’t you do content negotiation in WCF by setting automaticFormatSelectionEnabled to true as per the docs https://msdn.microsoft.com/en-us/library/ee476510.aspx ? Although I have no love of WCF and think ASP.NET Web API is a better choice I don’t think content negotiation is a good reason to base this choice on.

  5. Hello Sir,

    Thanks for very nice article and good explained.

    Regards
    Santhakumar

  6. Joseph Cooney is correct. WCF supports content negotiation starting with framework 4. The MSDN link gives a lot of detail on the subject, but I also found this article from Andrea Regoli to provide a good example of the different ways to configure WCF to provide content negotiation: https://andrearegoli.wordpress.com/2014/06/05/wcf-rest-configure-endopoint-to-return-soap-xml-and-json/#bywebconfig

  7. thanks for awesome article.
    please change this line By discussion so far we can say you don’t create REST Service using the WCF. instead of By discussion so far we can say you don’t create REST Service using the Web API.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com