I have been hooked on to REST lately. No i am not taking a rest as in a break but i am talking about the architectural pattern called REST i.e. Representation State Transfer in Service Oriented Applications. If you have been following my posts then you know that i have started to concentrate on REST specifically with Windows Communication Foundation as the technology.
As a prelude to this i would request you to have a look at my previous post Principles of REST. Principles of REST talked about the governing principles behind a RESTful service development. In this post i take an attempt in mapping those principles with the pieces in the technology i.e. WCF. So read on:
As gist to last post, the basic principles i talked about were:
Now lets see how each of these is addressed in WCF.
The whole concept of REST is every resource in your system is addressable by a URI. For e.g if you take the real world example of twitter, any user on twitter can be reached with by a simple URI which is http://twitter.com/kashyapa. Here kashyapa is my twitter handle and if you browse this URI you will get my profile view
Also if you want to check who is following me or who i follow, the URI is again so simple – http://twitter.com/#!/kashyapa/following
So the URI and URI processing is a very important piece in RESTful service development. In WCF the URI processing capabilities is provided by 2 classes. Namely: UriTemplate & UriTemplateTable. Taking the twitter examples if i were to create a WCF RESTful service which supports the above URL, the code will look like below
As you can see the URIs needs to be parsed and mapped to the right method and pass the right parameters to the method. So the UriTemplate and UriTemplateTable classes help to achieve these plumbing job. Will blog more on these classes in a future post.
HTTP methods are nothing but the verbs used to perform operations on the web. For e.g. if you type the following URL in an browser – http://twitter.com/kashyapa – in technical terms you are retrieving a resource. What happens in the background is, the browser issues a GET verb in the request and the WCF framework knows how to handle. Similarly whenever you are posting some form data the verb issued is POST in the request. Again the WCF framework knows how to handle this.
But how does all this work. What about other verbs available as part of HTTP. Well this is possible through the attribute annotation that we do on a web method. If you observe the previous section, the methods are all annotated with WebGetAttribute. Any other verb other than GET like POST, DELETE, UPDATE etc will be handled by another attribute WebInvokeAttribute.
Will look into these attributes in a future post.
Content type is nothing but the data with which the service has to work with. Basically this is dealing with the multiple data formats that the RESTful service has to support. For e.g. RESTful services will need to support the data exchange as a XML format or the popular JSON data object and other binary content such as images, videos etc.In WCF technology this is made possible by the binding and the behaviour that a service is configured to work with. So if you are familiar with the ABC concept of WCF, A stands for Address or WHERE, B stands for binding or HOW and C stands for Contract or WHAT. So binding and behaviour define how the communication takes place.
Again, will write a separate blog post on these.
In this blog post i have tried to map the principles of REST with the the technical pieces on the WCF technology that handle those principles. Hope this gives a head start on REST support in WCF. Stay tuned for more on this topic.
Till next time, as usual, happy Coding. Code with Passion, Decode with Patience.