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.
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.
A typical REST Service in WCF would look like as follows:
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,
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.
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.
There are four levels in the RMM from level 0 to level 3.
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,
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.
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.
A typical ASP.NET Web API may look as shown in the listing below,
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,
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,
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.