Click here to Skip to main content
13,296,377 members (51,940 online)
Click here to Skip to main content
Add your own
alternative version


445 bookmarked
Posted 30 Aug 2010

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.

You may also be interested in...


Comments and Discussions

GeneralHow to secure user info for authentication with wcf restful service Pin
farhat wazir21-Aug-17 23:16
memberfarhat wazir21-Aug-17 23:16 
Question2017-June-30 1135 - Issues in recreating your solution, thankfully your source was downloadable..... Shame. Pin
mrplatypus30-Jun-17 1:38
membermrplatypus30-Jun-17 1:38 
GeneralMy vote of 5 Pin
Jnnxp8-Jun-17 4:23
memberJnnxp8-Jun-17 4:23 
GeneralRe: My vote of 5 Pin
Member 1326337321-Jun-17 6:01
memberMember 1326337321-Jun-17 6:01 
GeneralRe: My vote of 5 Pin
Jnnxp21-Jun-17 10:33
memberJnnxp21-Jun-17 10:33 
GeneralRe: My vote of 5 Pin
Member 1326337322-Jun-17 1:08
memberMember 1326337322-Jun-17 1:08 
PraiseNice article.... Pin
Martin Goodrich13-Apr-17 10:40
memberMartin Goodrich13-Apr-17 10:40 
QuestionGood article Pin
lakshan Sakila20-Feb-17 19:51
memberlakshan Sakila20-Feb-17 19:51 
QuestionClient Side Call Pin
Member 78723438-Feb-17 19:42
memberMember 78723438-Feb-17 19:42 
Questionport 35798 : how to change it ? Pin
csthompsondo1-Feb-17 9:37
membercsthompsondo1-Feb-17 9:37 
QuestionHow do i pass two or more values from url?? Pin
Member 122632317-Dec-16 0:53
memberMember 122632317-Dec-16 0:53 
SuggestionPoint of Interest Pin
Daniel Kienböck26-Jun-16 11:40
memberDaniel Kienböck26-Jun-16 11:40 
PraiseVery clear article. Pin
krill25-Mar-16 12:45
professionalkrill25-Mar-16 12:45 
QuestionWant to access these services from web application Pin
Karteek Panakani12-Jan-16 0:28
memberKarteek Panakani12-Jan-16 0:28 
GeneralFast and Easy Pin
Corlionmao10-Dec-15 6:27
memberCorlionmao10-Dec-15 6:27 
QuestionVery nice article for beginners Pin
NishantRaval8-Oct-15 2:50
memberNishantRaval8-Oct-15 2:50 
QuestionThank you nice article... Pin
Karthikeyan Kandasamy22-Sep-15 21:54
memberKarthikeyan Kandasamy22-Sep-15 21:54 
QuestionBlank White Screen Pin
Debashish Sarkar19-Sep-15 8:31
memberDebashish Sarkar19-Sep-15 8:31 
QuestionThanks.. but there is a problem Pin
coolsun222-Sep-15 22:17
membercoolsun222-Sep-15 22:17 
QuestionMy vote of 5 Pin
Member 1186949128-Jul-15 8:32
memberMember 1186949128-Jul-15 8:32 
Questionport 35798 : how to change it ? Pin
Eherve8-Jul-15 7:45
memberEherve8-Jul-15 7:45 
QuestionHow can i create a restful service a round an ASP.NET web application? Pin
Member 105787333-Jun-15 0:30
memberMember 105787333-Jun-15 0:30 
Questionerror message Pin
Member 1141052212-Feb-15 18:29
memberMember 1141052212-Feb-15 18:29 
AnswerRe: error message Pin
Himanshupareek6616-Jun-17 21:57
memberHimanshupareek6616-Jun-17 21:57 
Questionthanks Pin
Bebosh26-Jan-15 14:32
memberBebosh26-Jan-15 14:32 
QuestionGreat job explaining Pin
RChattop27-Dec-14 0:11
memberRChattop27-Dec-14 0:11 
AnswerRe: Great job explaining Pin
Member 1146031817-Feb-15 21:11
memberMember 1146031817-Feb-15 21:11 
Suggestiongreat example Pin
Member 105935315-Nov-14 9:05
memberMember 105935315-Nov-14 9:05 
QuestionVisual Studio 2013 crash when creating a WCF Service Application Pin
azprivat11-Sep-14 3:46
memberazprivat11-Sep-14 3:46 
QuestionWhat do you mean by statelessness? Pin
B41329-Jul-14 0:55
memberB41329-Jul-14 0:55 
AnswerRe: What do you mean by statelessness? Pin
ashutosh k. shukla29-Jul-14 16:55
memberashutosh k. shukla29-Jul-14 16:55 
GeneralRe: What do you mean by statelessness? Pin
Mihai Barbos13-Aug-14 0:39
memberMihai Barbos13-Aug-14 0:39 
GeneralGreat example Pin
nicolas mertens24-Jun-14 2:07
membernicolas mertens24-Jun-14 2:07 
GeneralMy vote of 5 Pin
Awadhendra Tripathi8-Jun-14 21:32
professionalAwadhendra Tripathi8-Jun-14 21:32 
QuestionCreate RESTful WCF Service using VB.NET Pin
IshaqSalam8-Jun-14 20:02
memberIshaqSalam8-Jun-14 20:02 
QuestionProblem while returning URL like returns http:/\ in JSON Pin
pavan_wani0728-May-14 1:05
memberpavan_wani0728-May-14 1:05 
QuestionRESTSERVICE unable to return values (ReadResponse() failed: The server did not return a response for this request. Server returned 0 bytes) Pin
pavan_wani0728-May-14 1:00
memberpavan_wani0728-May-14 1:00 
QuestionAt the beginning I couldn't get /xml/123 or /json/123 Pin
Alex161128-Apr-14 9:55
memberAlex161128-Apr-14 9:55 
Questionuseful Pin
Oscarw22-Apr-14 6:20
memberOscarw22-Apr-14 6:20 
QuestionASMX Application Pin
vinodhini sekar1-Apr-14 21:00
membervinodhini sekar1-Apr-14 21:00 
AnswerRe: ASMX Application Pin
JRASPEN30-May-14 9:40
memberJRASPEN30-May-14 9:40 
GeneralThank you very much, great example (-: Pin
Ran Partush29-Mar-14 6:49
memberRan Partush29-Mar-14 6:49 
QuestionGreat example! Pin
Member 460685218-Mar-14 10:35
memberMember 460685218-Mar-14 10:35 
SuggestionMessage Closed Pin
5-Mar-14 1:26
professionalRavi.Kumar0215-Mar-14 1:26 
QuestionXML works, JSON fails Pin
kevininstructor4-Mar-14 10:43
memberkevininstructor4-Mar-14 10:43 
GeneralLean and kean Pin
Duncan Edwards Jones3-Mar-14 11:37
professionalDuncan Edwards Jones3-Mar-14 11:37 
QuestionMy vote of 5 Pin
kszynter19-Feb-14 21:52
memberkszynter19-Feb-14 21:52 
QuestionMy vote of 5 Pin
maiteib11-Feb-14 3:12
membermaiteib11-Feb-14 3:12 
GeneralMy vote of 4 Pin
Member 1024241426-Dec-13 23:42
memberMember 1024241426-Dec-13 23:42 
QuestionI am unable to get the output as shown here Pin
Dinesh9217-Dec-13 19:30
memberDinesh9217-Dec-13 19:30 

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 | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 30 Aug 2010
Article Copyright 2010 by ashutosh k. shukla
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid