Click here to Skip to main content
15,113,353 members
Articles / Programming Languages / C# 4.0
Posted 30 Aug 2010


463 bookmarked

Create RESTful WCF Service API: Step By Step Guide

Rate me:
Please Sign up or sign in to vote.
4.86/5 (262 votes)
30 Aug 2010CPOL4 min read
Step by Step Guide to create Restful WCF service API in ASP.NET and C#


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 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 the 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/retrieve information which is below:

  1. GET - Requests a specific representation of a resource
  2. PUT - Creates or updates a resource with the supplied representation
  3. DELETE - Deletes the specified resource
  4. 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 supposed to be accessed 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 are some points 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, a developer is scared to use WCF because of a 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.


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

Image 1


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.

Image 2


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


As I explained at the start 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 the following code:

Image 4

In the above code, you can see two different methods of IRestService which are XMLData and JSONData. XMLData returns result in XML whereas JSONData in JSON.


Open the file RestServiceImpl.svc.cs and write the following code over there:

Image 5


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.


This part contains information about the End Point. Below are the code details.

Click to enlarge image


This part contains details about service and endpoint behavior.

Image 7

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

Service Ready to Test Now

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

Image 8

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

Image 9

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


  • 30th August, 2010: Initial post


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


About the Author

ashutosh k. shukla
Software Developer (Senior)
Singapore Singapore
A versatile person having 10 Years of IT Industry experience in software development on .NET and Android Technology.

Comments and Discussions

GeneralRe: My Vote For 5 Pin
ashutosh k. shukla21-Feb-12 23:06
Memberashutosh k. shukla21-Feb-12 23:06 
QuestionConsume REST service Pin
tinh.khau16-Feb-12 7:23
Membertinh.khau16-Feb-12 7:23 
AnswerRe: Consume REST service Pin
ashutosh k. shukla16-Feb-12 7:27
Memberashutosh k. shukla16-Feb-12 7:27 
GeneralMy vote of 5 Pin
chavahgr15-Feb-12 18:27
Memberchavahgr15-Feb-12 18:27 
GeneralRe: My vote of 5 Pin
ashutosh k. shukla16-Feb-12 7:30
Memberashutosh k. shukla16-Feb-12 7:30 
GeneralMy vote of 1 Pin
scot_dotNet24-Jan-12 10:02
Memberscot_dotNet24-Jan-12 10:02 
GeneralRe: My vote of 1 Pin
ashutosh k. shukla24-Jan-12 19:08
Memberashutosh k. shukla24-Jan-12 19:08 
GeneralMy vote of 4 Pin
Thiago R. Santos11-Jan-12 12:53
MemberThiago R. Santos11-Jan-12 12:53 
really nice
GeneralMy vote of 5 Pin
faheemsial22-Dec-11 1:37
Memberfaheemsial22-Dec-11 1:37 
GeneralMy vote of 5 Pin
Ratnesh N Bharos30-Nov-11 3:05
MemberRatnesh N Bharos30-Nov-11 3:05 
GeneralMy vote of 5 Pin
canozurdo22-Nov-11 11:01
Membercanozurdo22-Nov-11 11:01 
GeneralNice... Pin
Tate Antrim25-Oct-11 8:36
professionalTate Antrim25-Oct-11 8:36 
GeneralMy vote of 5 Pin
theBigKauuna14-Oct-11 10:40
MembertheBigKauuna14-Oct-11 10:40 
Questionhow to remove JSONDataResult? Pin
r0lan212-Oct-11 18:56
Memberr0lan212-Oct-11 18:56 
AnswerRe: how to remove JSONDataResult? Pin
ashutosh k. shukla12-Oct-11 20:12
Memberashutosh k. shukla12-Oct-11 20:12 
AnswerRe: how to remove JSONDataResult? Pin
klaus knudsen25-Oct-11 12:52
Memberklaus knudsen25-Oct-11 12:52 
GeneralRe: how to remove JSONDataResult? Pin
ashutosh k. shukla25-Oct-11 16:45
Memberashutosh k. shukla25-Oct-11 16:45 
GeneralRe: how to remove JSONDataResult? Pin
Member 842756423-Nov-11 20:09
MemberMember 842756423-Nov-11 20:09 
GeneralMy vote of 5 Pin
r0lan212-Oct-11 18:54
Memberr0lan212-Oct-11 18:54 
GeneralMy vote of 5 Pin
Pauwels Bart3-Oct-11 0:07
MemberPauwels Bart3-Oct-11 0:07 
QuestionGreat Article Pin
Aleks7629-Sep-11 14:44
MemberAleks7629-Sep-11 14:44 
AnswerRe: Great Article Pin
ashutosh k. shukla29-Sep-11 18:20
Memberashutosh k. shukla29-Sep-11 18:20 
GeneralMy vote of 5 Pin
tetzschner15-Jul-11 6:01
Membertetzschner15-Jul-11 6:01 
GeneralAn obvious 5 Pin
bhanuj25-May-11 2:20
Memberbhanuj25-May-11 2:20 
GeneralMy vote of 5 Pin
AhsanS16-May-11 20:56
MemberAhsanS16-May-11 20:56 

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.