Wednesday, 1 August 2012

WCF: REST

What is REST?

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations. REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.). Later, we will see how much more simple REST is.Despite being simple, REST is fully-featured; there's basically nothing you can do in Web Services that can't be done with a RESTful architecture.

Who's using REST?


All of Yahoo's web services use REST, including Flickr, del.icio.us API uses it, pubsub, bloglines, technorati, and both eBay, and Amazon have web services for both REST and SOAP.


Who's using SOAP?


Google seems to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprise software as well.


REST vs. SOAP

The main advantages of REST web services are:
  • Lightweight - not a lot of extra xml markup
  • Human Readable Results
  • Easy to build - no toolkits required

 SOAP also has some advantages:
  • Easy to consume - sometimes
  • Rigid - type checking, adheres to a contract
  • Development tools
For consuming web services, its sometimes a toss up between which is easier. For instance Google's Ad-words web service is really hard to consume (in CF anyways), it uses SOAP headers, and a number of other things that make it kind of difficult. On the converse, Amazon's REST web service can sometimes be tricky to parse because it can be highly nested, and the result schema can vary quite a bit based on what you search for.


Which ever architecture you choose make sure its easy for developers to access it, and well documented.


How to define a service as REST based service in WCF?

  WCF 3.5 provides explicit support for RESTful communication using a new binding named WebHttpBinding. 

The below code shows how to expose a RESTful service 

[ServiceContract]


interface IStock


{


[OperationContract]


[WebGet]


int GetStock(string StockId);


}


By adding the WebGetAttribute, we can define a service as REST based service that can be accessible using HTTP GET operation.


No comments:

Post a Comment