Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET

CREATE RESTful WCF Service API Using POST: Step By Step Guide

Rate me:
Please Sign up or sign in to vote.
4.71/5 (49 votes)
25 May 2011CPOL4 min read 452.2K   17.2K   100   61
This article will help you to create RESTful WCF POST API. I have given Client and Server example which is really easy to understand.

Introduction

In my previous article, I tried 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. In this article, we will create one WCF RESTFul POST API. I will also try to explain why and where to use POST method.

Why to use POST, not GET?

When I write any WCF service, I always use POST. The reason is the advantage of POST over GET. Using HTTP POST method, you can almost achieve everything which you can achieve from GET. Despite this, you will get some additional feature if using 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.

POST

  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

GET

  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 an 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 it 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. URI 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.

Step by Step Guide

STEP 1

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

Image 1

STEP 2

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.

Step-2

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”.

Image 3

STEP 4

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

Step-4

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 the same as RequestData. So the post XML request format can be like below:

Step-4

We must have to use namespace for the data contract in POST RESTFul service, which has multiple uses, i.e., reflects the current version of your code. The same namespace should use in XML to Post data. Here, I have given http://www.eysnap.com/mPlayer as namespace which can be changed.

STEP 5

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

Image 6

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

STEP 6

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

STEP-6

STEP 7

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

Services

Image 8

Behaviors

Image 9

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 the correct one.

Image 10

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.
Thanks.

History

  • 25th May, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://mr-ashu.blogspot.com/
Singapore Singapore
A versatile person having 10 Years of IT Industry experience in software development on .NET and Android Technology.

Comments and Discussions

 
Questionxmlns in request format Pin
Member 1568561123-Jun-22 21:18
Member 1568561123-Jun-22 21:18 
QuestionHow I convert xml data with xmlns to JSON? Pin
Hossein Ganjyar25-Nov-17 10:36
Hossein Ganjyar25-Nov-17 10:36 
QuestionCan you show an example using JSON Pin
mrbonny72-Nov-17 3:12
mrbonny72-Nov-17 3:12 
QuestionHow to get data in json format Pin
Sai Pawar19-Jul-17 18:50
professionalSai Pawar19-Jul-17 18:50 
AnswerRe: How to get data in json format Pin
mrbonny72-Nov-17 3:22
mrbonny72-Nov-17 3:22 
GeneralMy vote of 4 Pin
ZurdoDev25-Jan-17 8:52
professionalZurdoDev25-Jan-17 8:52 
GeneralMy vote of 5 Pin
NF Khan5-Nov-16 22:59
NF Khan5-Nov-16 22:59 
QuestionHow do enable metadata for this service? How do i test this service using SOAPUI? Pin
Member 104537273-Jun-16 22:11
Member 104537273-Jun-16 22:11 
Questionnot working. getting metadata unaccessible error Pin
Member 115702426-Apr-16 3:10
Member 115702426-Apr-16 3:10 
SuggestionAbove code and Fourthbottle code helped me a lot to know WCF Pin
lawrine7-Oct-15 4:01
lawrine7-Oct-15 4:01 
QuestionHow to Invoke this Service? Pin
Member 1193173016-Sep-15 5:28
Member 1193173016-Sep-15 5:28 
QuestionMOre than one row in post data is not showing up in Auth Pin
mexidamus7-Aug-15 7:43
mexidamus7-Aug-15 7:43 
QuestionWhy to use POST, not GET? Pin
ali.adravi22-Jul-15 4:39
ali.adravi22-Jul-15 4:39 
Questionworking fyn Pin
Rohith Kr6-Mar-15 7:22
Rohith Kr6-Mar-15 7:22 
Questioncrd operations Pin
Member 91711281-Jul-14 9:25
Member 91711281-Jul-14 9:25 
QuestionSoapAction in RESTful WCF service API using POST Pin
riya__yari9-May-14 21:25
riya__yari9-May-14 21:25 
GeneralCREATE RESTful WCF Service API Using Post... Wonderful guide! Pin
LisaSnyder12-Mar-14 4:41
LisaSnyder12-Mar-14 4:41 
Question[My vote of 1] : Not very good Pin
msdevtech6-Sep-13 16:05
msdevtech6-Sep-13 16:05 
QuestionUnable to POST - Method Now Allowed Pin
Saad Anees28-May-13 21:07
Saad Anees28-May-13 21:07 
GeneralMy vote of 4 Pin
rajeshpmarathe22-Feb-13 0:16
rajeshpmarathe22-Feb-13 0:16 
Questionhow to call XMl file contain different tags Pin
Ramya singam5-Feb-13 19:41
Ramya singam5-Feb-13 19:41 
GeneralMy vote of 5 Pin
eds.net28-Jan-13 5:46
eds.net28-Jan-13 5:46 
GeneralMy vote of 5 Pin
SuperDuperPengu14-Jan-13 16:20
SuperDuperPengu14-Jan-13 16:20 
GeneralMy vote of 1 Pin
Member 813298511-Jul-12 20:48
Member 813298511-Jul-12 20:48 
GeneralHow to call xml directly Pin
Alpesh Dhanesha7-Jul-12 21:34
Alpesh Dhanesha7-Jul-12 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.