Monday, August 30, 2010

Create RESTful WCF Service API : Step By Step Guide

Introduction
Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for your services, enabling you to expose CLR types as services, and to consume other services as CLR types. In this article I am going to explain you how to implement restful service API using WCF 4.0 . The Created API returns XML and JSON data using WCF attributes.

What is REST?
Based on Roy Fielding theory "Representational State Transfer (REST), attempts to codify the architectural style and design constraints that make the Web what it is. REST emphasizes things like separation of concerns and layers, statelessness, and caching, which are common in many distributed architectures because of the benefits they provide. These benefits include interoperability, independent evolution, interception, improved scalability, efficiency, and overall performance."
Actually only the difference is how clients access our service. Normally, a WCF service will use SOAP, but if you build a REST service clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc).
REST uses some common HTTP methods to insert/delete/update/retrive infromation which is below:
a) GET - Requests a specific representation of a resource
b) PUT - Create or update a resource with the supplied representation
c) DELETE - Deletes the specified resource
d) POST - Submits data to be processed by the identified resource

Why and where to use REST?
Few days back i was writing a service which was suppose to access by heterogeneous language/platform/system. It can be used by iPhone, Android, Windows Mobile, .NET web application, JAVA or PHP. Using web service it was bit complex for me to expose it to everyone using uniform system. Then we decided to use REST, which was easily espoused over cloud. This was a great example which shows the capability of SIMPLE RESTful SERVICE :) . Below is some point which will help you to understand why to use the RESTful services.
1) Less overhead (no SOAP envelope to wrap every call in)
2) Less duplication (HTTP already represents operations like DELETE, PUT, GET, etc. that have to otherwise be represented in a SOAP envelope).
3) More standardized - HTTP operations are well understood and operate consistently. Some SOAP implementations can get finicky.
4) More human readable and testable (harder to test SOAP with just a browser).
5) Don't need to use XML (well you kind of don't have to for SOAP either but it hardly makes sense since you're already doing parsing of the envelope).
6) Libraries have made SOAP (kind of) easy. But you are abstracting away a lot of redundancy underneath as I have noted. Yes in theory SOAP can go over other transports so as to avoid riding atop a layer doing similar things, but in reality just about all SOAP work you'll ever do is over HTTP.

Step by step guide
Generally developer scared to use WCF because lot of confusing configuration. I will try to use minimum configuration so that it will be easier to understand for us. We will develop Restful WCS API in 6 steps. So let’s start now.

STEP:1) First of all launch Visual Studio 2010. Click FILE->NEW->PROJECT. Create new "WCF Service Application" .














STEP-2) Once you create project, you can see in solution that By Default WCF service and interface file is already created. Delete By default created file as we will create our own interface and WCF service file.


STEP-3) now Right click on solution and create one new WCF service file. I have given name to the service file as “RestServiceImpl.svc”.


STEP-4) As i explained in starting of the article that we will be writing an API which can return data in XML and JSON format, here is the interface for that. In IRestServiceImpl , add following code.


In above code, you can see two different method of IRestService which is XMLData and JSONData. XMLData Returns result in XML whereas JSONData in JSON.

STEP-5) Open the file RestServiceImpl.svc.cs and write following code over there..



STEP-6) Now let’s move to configuration part which is the last one. There will be two basic part of the configuration file which we must have to understand...

This part contains information about the End Point. below is the code detail..




This part contains details about service and endpoint behavior.


And that’s it. Our Restful WCF service is ready for test purpose.

Service Ready to Test Now
Now i launch the application in browser to see the result. I launch this service in IE and my URL is now http://localhost:35798/RestServiceImpl.svc . Now if i use http://localhost:35798/RestServiceImpl.svc/xml/123 URL, i get following response on the browser which an XML format and that was my task to achieve.


Now if i use http://localhost:35798/RestServiceImpl.svc/json/123 URL, i get following response on the browser which an XML format and that was also my task to achieve.


Hope the article is useful for community. You can download this code from here. Comments, suggestions and criticisms are all welcome.