Friday, June 18, 2010

How To Create and Consume REST Web Service using WCF

Recently i was asked to create a REST web service using WCF. So i have asked my best friend google about this.. but didn't get any working solution for that. After a long research i got REST webservice working. So now i would like to share the things which i have learnt while creating a REST webservice

First thing is to add WCF Project to your Solution

Just right click on the solution and select " ADD -> New Project -> WCF Service Application "

Changes to be done in Web.Config File

By default you will find the end-point binding as "wsHttpBinding", for REST Web service we need to use the end-point as "webHttpBinding".

Steps for making the above changes
Find System.ServiceModel -> behaviours and add the following end-point behaviour






Find the following code in system.servicemodel -> services

....

change it to

....


At this point of time. Build your solution once and verify everything is working properly. To verify, right click on .svc file(By default it will be Service1.svc) and view it in browser. If the service runs with out throwing any error then you are through the first phase of creating the REST Web service.

Now we will get into coding part


In your WCF Service, find the Interface Iservice.cs, In the Interface Iservice.cs you can define the methods and Uri template for each method,

UriTemplate defines the format of the Url with which we can access that perticular method in the service. We use WebGet method to get the url and match with the UriTemplates in the service. (Note: you need to add reference of System.ServiceModel.Web.dll for WebGet Method)

for example: If you want a method to give your full name by sending FirstName and LastName as parameters, then you can define the UriTemplate as "UriTemplate={firstname}/{lastname}"
where the values in the paranthasis are the parameters for that particular service.
Below you can find the method (CallRestService(string firstname, string lastname)) and its Uri Template.


Now you are 1 step away from creating a REST web service, you need to define the method(CallRestService(string firstname, string lastname) in Service1.svc.cs file.




rebuild your solution and thats you have created a REST web service which takes two input parameters and returns a string by concatenating two strings.
try to run your webservice and append two parameters first name and last name then hit enter you will see your full name as response from the webservice

In my case my service is running on localhost port 1908,
http://localhost:1908/Service1.svc

I need to enter parameters in the url to hit that perticular method
http://localhost:1908/Service1.svc/Kranthi/Kiran

yeah.. i got the response from the webservice


Now we need to call this Webservice from the WebApplication, This will be an easy task for us. As we need to create a proxy for WebClient to call the service and just hit the URL http://localhost:1908/Service1.svc/Kranthi/Kiran and get response



So here on the page loaded event i am calling the webservice using Webclient DownloadStringCompletedAsync function which takes the uri to be hit and UriKind.
In our case we are using Absolute Uri.
On DownloadString completed eventhandler we will be getting our result in the EventArgs. So e.Result contains the response from the service.

So the basic steps to create and consume the webservice are:
1) Add WCF project to your solution
2) Modify the Web.Config file in the service, changing the endpoint behaviors
3) define the UriTemplate in the interface and define the service method in Service.svc.cs file
4)Create a proxy in the web application.
5) Send the request and get the response

Thats it for now.. i will be updating the blog with how to consume the created REST webservice soon.... :)