Click here to Skip to main content
Click here to Skip to main content

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#

Introduction

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 Smile | :) . 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.

STEP 1

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

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 3

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

STEP 4

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.

STEP 5

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

STEP 6

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.

<services> 

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

Click to enlarge image

<behaviors>

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.

History

  • 30th August, 2010: Initial post

License

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

Share

About the Author

ashutosh k. shukla
Software Developer (Senior) http://mr-ashu.blogspot.com/
Singapore Singapore
A versatile person having 6.0 Years of IT Industry experience in software development on .NET Technology presently leading the team successfully.
Follow on   Twitter

Comments and Discussions

 
QuestionWhat do you mean by statelessness? PinmemberB41328-Jul-14 23:55 
AnswerRe: What do you mean by statelessness? Pinpremiumashutosh k. shukla29-Jul-14 15:55 
GeneralRe: What do you mean by statelessness? PinmemberMihai Barbos12-Aug-14 23:39 
GeneralGreat example Pinmembernicolas mertens24-Jun-14 1:07 
GeneralMy vote of 5 PinprofessionalAwadhendra Tripathi8-Jun-14 20:32 
QuestionCreate RESTful WCF Service using VB.NET PinmemberIshaqSalam8-Jun-14 19:02 
QuestionProblem while returning URL like http://www.codeproject.com returns http:/\www.codeproject.com in JSON Pinmemberpavan_wani0728-May-14 0:05 
QuestionRESTSERVICE unable to return values (ReadResponse() failed: The server did not return a response for this request. Server returned 0 bytes) Pinmemberpavan_wani0728-May-14 0:00 
QuestionAt the beginning I couldn't get /xml/123 or /json/123 PinmemberAlex161128-Apr-14 8:55 
Questionuseful PinmemberOscarw22-Apr-14 5:20 
QuestionASMX Application Pinmembervinodhini sekar1-Apr-14 20:00 
AnswerRe: ASMX Application PinmemberJRASPEN30-May-14 8:40 
GeneralThank you very much, great example (-: [modified] PinmemberRan Partush29-Mar-14 5:49 
QuestionGreat example! PinmemberMember 460685218-Mar-14 9:35 
SuggestionMessage Automatically Removed PinprofessionalRavi.Kumar0215-Mar-14 0:26 
QuestionXML works, JSON fails [modified] Pinmemberkevininstructor4-Mar-14 9:43 
GeneralLean and kean PinprofessionalDuncan Edwards Jones3-Mar-14 10:37 
QuestionMy vote of 5 Pinmemberkszynter19-Feb-14 20:52 
QuestionMy vote of 5 Pinmembermaiteib11-Feb-14 2:12 
GeneralMy vote of 4 PinmemberMember 1024241426-Dec-13 22:42 
QuestionI am unable to get the output as shown here PinmemberDinesh9217-Dec-13 18:30 
QuestionVery good starting point PinmemberMember 806624214-Nov-13 4:18 
GeneralMy vote of 5 Pinmembermvrene12-Nov-13 13:37 
QuestionMethod not allowed Pinmembervvdhar21-Oct-13 5:39 
AnswerRe: Method not allowed Pinmemberjo1322-Nov-13 1:53 
GeneralMy vote of 5 PinmemberMario Gagnon20-Oct-13 5:43 
General5 Stars!!! PinmemberMember 1015311314-Oct-13 5:37 
GeneralVery Nice Article PinmemberJaymala Singh14-Oct-13 3:28 
QuestionHow can i send secure data in WCF Pinmemberluckyjamal6-Oct-13 13:32 
AnswerRe: How can i send secure data in WCF PinmemberDinesh9217-Dec-13 18:41 
QuestionNot localhost PinmemberMMessell11-Sep-13 2:38 
AnswerRe: Not localhost Pinmemberashutosh k. shukla15-Sep-13 20:39 
GeneralMy vote of 5 PinmemberMember 78137196-Sep-13 2:13 
QuestionHow do I customize the response? Pinmembereeidfn20-Aug-13 20:26 
QuestionQuestion in Steep 6 PinmemberTúlio Cruz8-Jul-13 7:29 
GeneralMy vote of 4 PinmemberPiyush Trada4-Jul-13 3:52 
QuestionCannot get the function work Pinmemberqingdaimsft4-Jul-13 0:40 
AnswerRe: Cannot get the function work Pinmembermti027-Aug-13 10:57 
GeneralMy vote of 5 Pinmembertolleyfam6-Jun-13 6:11 
GeneralMy vote of 3 PinmemberDebojyoti Saha4-Jun-13 21:04 
GeneralMy vote of 5 PinmemberMember 32777602-Jun-13 22:10 
GeneralMy vote of 5 Pinmemberanurag_bhd17-May-13 20:01 
GeneralMy vote of 5 PinmemberCristian Amarie28-Apr-13 0:03 
QuestionPost call with soap envelope PinmemberMember 34719682-Apr-13 17:35 
Questioncall post with soap envelope PinmemberMember 34719682-Apr-13 17:17 
GeneralMy vote of 5 PinmemberMember 451897627-Mar-13 21:04 
GeneralMy vote of 5 Pinmembermehtabalikhan14-Mar-13 23:53 
GeneralMy vote of 4 Pinmemberbindugadu5-Mar-13 3:15 
QuestionConsuming Restful Service PinmemberLalit227-Feb-13 1:03 
QuestionSecurity Question PinmemberMatthysDT25-Feb-13 21:05 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 30 Aug 2010
Article Copyright 2010 by ashutosh k. shukla
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid