Wednesday, May 25, 2011

CREATE WCF POST Restful service


In my last article to explain about WCF Restful service using HTTP Get method.  This works well as long as you are sending small data as information to the service. But if want to deliver huge data, HTTP GET Method is not a good choice.  

When i write any WCF service, i always use POST. Reason is the advantage of POST over GET. Using HTTP POST method, you can almost achieve everything which you can achieve from GET. Despite of this you will get some additional feature if use POST. We might say that "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail etc. 
1) Easy Character Encoding using application/x-www-form-urlencoded
2) No Proxy by default so always actual data from web server. 
3) Data length can be restricted by webserver, not by browser.

1) Character encoding will reduce the amount of data that can be used because of url encoding entities (i.e. three Japanese characters are converted to this: %26%2312454%3B%26%2312455%3B%26%2312502%3B)
2) Running a http request with GET can be cached on your web browser or a configured proxy server.
3) Maximum URL length is 2,083 characters in Internet Explorer (see MaxClientRequestBuffer: Use POST instead of GET to Send Large Amounts of Data in Request)
and lot more  
Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Sending long information via URL is not a good way of implementation and also there has many restrictions i.e. max length of URL, Information format bla bla bla. For example Internet Explorer has a limitation implemented at 2083 characters. URIs is meant to be readable not to send information. 
So if you are writing any REST service and your information is long enough, better to choose POST instead of GET method.
In this article, i am going to create WCF Restful service using POST method and access it using HTTP Request. So we will have client and server both in this example code. I am creating one service which accepts HTTP Post XML request and response request data in XML format.  

Launch Visual Studio 2010. Click FILE->NEW->PROJECT. Create new "WCF Service Application". .


Once you create the project, you can see in solution that By Default WCF service and interface file are already created. Delete By default created file as we will create our own interface and WCF service file.

Now right click on solution and create one new WCF service file. I have given name to the service file as “RestServiceImpl.svc”.

We will create two DataContract classes for request and response purpose.

RequestData will receive the request from the client and ResponseData response. One very important thing is that POST XML format to the service should be same as RequestData. So the post xml request format can be like below:  

We must have to use namespace for the data contract in POST RESTFul service, which have multiple use i.e. reflects the current version of your code. The Same namespace should use in XML to Post data. Here i have given as namespace which can be changed.

Now we will write the OperationContract in IRestServiceImpl which is an interface.  Below is the code for that. 

As we can see in above code that here, Request and Response format is XML. We can change it to JSON also by changing the WebMessageFormat  from WebMessageFormat.Xml to WebMessageFormat.Json 

Implementation of Auth OperationContract in RestServiceImpl.svc.cs here.

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


And that’s it. Our Restful WCF POST RESTful service is ready to test. SERVICE IS READY I have created one Test WebClient in C# which makes Http request with some XML data. My application is hosted on localhost and URL is http://localhost:35798/RestServiceImpl.svc/auth . I launched and invoked service using WebClient. Below is my XML response which is correct one.

This service can be used by any language application (.NET, PHP, JAVA etc) and that is the major reason to use REST service in the application. 

Hope the article is useful for the community. Comments, suggestions and criticisms are all welcome.

You can download the source code from here .   

Thanks a Lot



  1. Ashu, very useful post. As you mentioned that POST can pass huge data to the server than GET. The most usefulness of the REST service comes in when the service can be consumed by any platform and we don't have to rely on proxy classes anymore.

    Waiting for more posts from you.. :-)

    Cheers !!

  2. Hi Ashu,

    Thnx for the post.
    i have created the service as you said not able to consume it using webclient POST method.

    wen i tried to send XML input it is giveng bad request error.

    Can you please share the client code that you used to test above service.

    thnx in advance.

  3. Hi Ashu,

    I was follow your step by step to create WCF RestFul Service, but I recreate project using VB.NET language.
    but I get some problem
    1. when i want to test from URL localhost:6017/RestServiceImpl.svc/xml/123, do nothing (no result)
    2. when I using IIS and I test again localhost/RestService//RestServiceImpl.svc/xml/123, do nothing too

    can You help me?