Click here to Skip to main content
14,207,783 members
Click here to Skip to main content
Add your own
alternative version


461 bookmarked
Posted 30 Aug 2010
Licenced CPOL

Create RESTful WCF Service API: Step By Step Guide

, 30 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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".


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


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:

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:


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.

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.

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.

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

Generalsome errors r rising Pin
siva1614-Jun-12 21:01
membersiva1614-Jun-12 21:01 
QuestionServer Error in '/' Application. Pin
breadjin14-Jun-12 16:09
memberbreadjin14-Jun-12 16:09 
QuestionHow to return data for jsonp calls Pin
Dexter1111-Jun-12 20:27
memberDexter1111-Jun-12 20:27 
AnswerRe: How to return data for jsonp calls Pin
ashutosh k. shukla12-Jun-12 13:31
memberashutosh k. shukla12-Jun-12 13:31 
QuestionGood job Pin
B1gtech4-Jun-12 11:46
memberB1gtech4-Jun-12 11:46 
AnswerRe: Good job Pin
ashutosh k. shukla4-Jun-12 17:24
memberashutosh k. shukla4-Jun-12 17:24 
Generalthanks Pin
mustika753-Jun-12 22:04
membermustika753-Jun-12 22:04 
GeneralRe: thanks Pin
ashutosh k. shukla4-Jun-12 1:59
memberashutosh k. shukla4-Jun-12 1:59 
QuestionWorks great on local machine, but 404 on prod server Pin
caseycrookston28-May-12 8:04
membercaseycrookston28-May-12 8:04 
AnswerRe: Works great on local machine, but 404 on prod server Pin
ashutosh k. shukla4-Jun-12 1:59
memberashutosh k. shukla4-Jun-12 1:59 
GeneralMy vote of 5 Pin
BillTns18-May-12 6:26
memberBillTns18-May-12 6:26 
QuestionSome questions Pin
BillTns18-May-12 6:22
memberBillTns18-May-12 6:22 
GeneralMy vote of 5 Pin
EbrahimAsadi13-May-12 5:18
memberEbrahimAsadi13-May-12 5:18 
GeneralRe: My vote of 5 Pin
ashutosh k. shukla13-May-12 20:45
memberashutosh k. shukla13-May-12 20:45 
Questionfor .net framework 3.5 Pin
Sumit Saha Roy9-May-12 21:44
memberSumit Saha Roy9-May-12 21:44 
AnswerRe: for .net framework 3.5 Pin
ashutosh k. shukla13-May-12 20:44
memberashutosh k. shukla13-May-12 20:44 
GeneralMy vote of 5 Pin
Member 18864728-May-12 7:40
memberMember 18864728-May-12 7:40 
GeneralRe: My vote of 5 Pin
ashutosh k. shukla9-May-12 19:59
memberashutosh k. shukla9-May-12 19:59 
QuestionNice Pin
Nachiket Marathe29-Apr-12 19:03
memberNachiket Marathe29-Apr-12 19:03 
QuestionJust what I needed Pin
ukrick17-Apr-12 5:04
memberukrick17-Apr-12 5:04 
AnswerRe: Just what I needed Pin
ashutosh k. shukla17-Apr-12 5:05
memberashutosh k. shukla17-Apr-12 5:05 
Questionhow about post method Pin
DL55-Apr-12 6:17
memberDL55-Apr-12 6:17 
AnswerRe: how about post method Pin
ashutosh k. shukla5-Apr-12 7:29
memberashutosh k. shukla5-Apr-12 7:29 
Questionnice article Pin
samiran bhuin3-Apr-12 0:20
membersamiran bhuin3-Apr-12 0:20 
QuestionNice Pin
Member 40480202-Apr-12 1:18
memberMember 40480202-Apr-12 1:18 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190612.1 | Last Updated 30 Aug 2010
Article Copyright 2010 by ashutosh k. shukla
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid