REST stands for Representational State Transfer. This is a protocol for exchanging data over a distributed environment. The main idea behind REST is that we should treat our distributed services as a resource and we should be able to use simple HTTP protocols to perform various operations on that resource.
When we talk about the Database as a resource we usually talk in terms of CRUD operations. i.e. Create, Retrieve, Update and Delete.
REST architecture focuses almost on the same set of constraints like Uniform interface, separation of concerns, caching, statelessness etc. as other distributed architecture follow.
Using REST, we get the following advantages:
- Interoperability
- Scalability
- Performance
- Efficiency
- Independent Evolution
- and many more…
REST also means using HTTP the way it meant to be. But a simple WCF service uses HTTP only as a transport, although HTTP is much more than just a transport. HTTP is already designed in the way as RESTful architecture suggests. RESTful services are also referred as HTTP services because HTTP also focuses on interaction between resources and their representation.
HTTP is the standard protocol for the all communication between resources over the Web. So, it defines various methods for interaction with resources as follows:
- GET:- means requesting a specific representation of a resource.
- PUT:- means creating or updating a resource with a provided representation.
- DELETE:- simply defines, deleting the specific resource.
- POST:- defines submitting data to be processed by the identified resource.
- HEAD:- is similar to GET but only retrieve header not the body.
- OPTIONS:- it returns the method supported by the identified resource.
RESTful services are those which follow the REST (Representational State Transfer) architectural style. Before implementing your first RESTful service, lets first understand the concept behind it. As we know that WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that “Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls”.
webHTTPBinding is the binding used for RESTful services.
[ServiceContract]
interface ICustomer
{
//”View It” -> HTTP GET
[WebGet( UriTemplate=”customers/{id}” )]
Customer GetCustomer( string id ):
//”Do It“ -> HTTP PUT
[WebInvoke( UriTemplate=”customers/{id}”, Method=”PUT” )]
Customer UpdateCustomer( string id, Customer newCustomer );
}
Web Service
- It is based on SOAP and return data in XML form.
- It support only HTTP protocol.
- It is not open source but can be consumed by any client that understands xml.
- It can be hosted only on IIS.
WCF
- It is also based on SOAP and return data in XML form.
- It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
- The main issue with WCF is, its tedious and extensive configuration.
- It is not open source but can be consumed by any client that understands xml.
- It can be hosted with in the applicaion or on IIS or using window service.
WCF Rest
- To use WCF as WCF Rest service you have to enable webHttpBindings.
- It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
- To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
- Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
- It support XML, JSON and ATOM data format.
Web API
- This is the new framework for building HTTP services with easy and simple way.
- Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
- Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
- It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
- It can be hosted with in the application or on IIS.
- It is light weight architecture and good for devices which have limited bandwidth like smart phones.
- Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
To whom choose between WCF or WEB API
- Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
- Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
- Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
- Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.