Friday, April 19, 2013

Simple Steps to Create RESTful Service in WCF

REST defines a set of architectural principles by which you can design Web services that focus on a system's resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages.
REST also facilities the transactions between web servers and allow loose coupling between different services or clients. REST is less strongly typed then SOAP.

There are many RESTful services provided by many service providers for different purposes. You can check the list of some of the popular services from here.

I am not giving lots of theoretical update on RESTful service but here I am trying to describe simple steps on “How to create RESTful service”

In my previous post wcf-simple-steps-to-remove-tempuri  I describe the basic steps to create simple service with “webHttpBinding”.

So let’s proceed further and add some missing steps for exposing restful address.

1.       As shown in the screen shot below, first of all we need to add namespace “System.ServiceModel.Web” in contract file. This namespace have declaration of WebInvoke attribute.

In WebInvoke attribute we define the verb, “POST”, “GET”, “PUT” or “DELETE”, as supported by method. And we can declare the access point of the method. That, I’ll explain at the time of calling the service. This time I want to post the data using the “interface” in URL instead of GetData (actual method name).

2.       Once you add the attribute in contract you need to check the web.config if service has the endpoint with “webhttpBinding” as

<endpoint address="Api" binding="webHttpBinding" contract="MyRestfulService.IPost"
behaviorConfiguration="restfulBehaviour" bindingNamespace="http://localhost/Services/MyRestfulService/2013/04/">

address="Api" : is to define the access point in address and you will be able to access the restful like http://localhost:39478/MyPosts.svc/Api/interface

binding="webHttpBinding" : to create restful service you need to define binding as “webHttpBinding”

behaviorConfiguration="restfulBehaviour" is to define the behavior of the endpoint which may have the single tag webHttp e.g.

              <behavior name="restfulBehaviour">
                     <webHttp helpEnabled="true" />

In this endpoint behavior configuration helpEnabled="true" play an important role in case of validating the address and to get the information about the methods this restful service support and the parameters of every method.

It also provides the sample request format which helps you to prepare the request for any method.
In production environment we shall recommend you to set helpEnabled="false" for security reason.

3.       After doing these configurations. Press CTRL +F5 to run this Service.

4.       In your browser window after the http://localhost:39478/MyPosts.svc put “/API/Help”
You will see the help window which shows the detail about the methods and signature of the methods your service is supporting

Please refer the screen shot below

We add “API” in url because we have added address="Api" in endpoint configuration in web.config. By the way you can change it as you want. Then the access point will be changed

5.       Once you see the screen shown in step 4 click on “POST” verb. You will see the complete help and url of the exposed method with a sample request.

So if it works like this your Restful service is ready now.

In next session I’ll describe how totest RESTful service

Post a Comment