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

Create RESTful WCF Service API: Step By Step Guide

By , 30 Aug 2010
 

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

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: I get "400 Bad Request"membervijiniro3 Nov '12 - 22:57 
QuestionCan i return an XML file ?memberpritam hinger11 Oct '12 - 21:25 
AnswerRe: Can i return an XML file ?memberashutosh k. shukla11 Oct '12 - 21:53 
GeneralRe: Can i return an XML file ?memberpritam hinger11 Oct '12 - 23:03 
GeneralRe: Can i return an XML file ?memberashutosh k. shukla11 Oct '12 - 23:48 
Questionerror while creating the Rest service [modified]memberkrishnagrandhi4 Oct '12 - 20:40 
AnswerRe: error while creating the Rest servicememberashutosh k. shukla4 Oct '12 - 21:03 
There may be many reason.
 
1) Your URL may not be correct.
2) The Contract Method Name is not define/correct .
 
Please follow the instruction step by step.
 
Thanks
Ashu
GeneralRe: error while creating the Rest servicememberkrishnagrandhi4 Oct '12 - 21:26 
GeneralRe: error while creating the Rest servicememberashutosh k. shukla4 Oct '12 - 23:12 
GeneralMy vote of 5memberMember 32777601 Oct '12 - 1:53 
GeneralMy vote of 5memberMaxwell Weru24 Sep '12 - 23:07 
GeneralRe: My vote of 5memberashutosh k. shukla25 Sep '12 - 20:02 
GeneralMy vote of 5memberRahul.RK23 Aug '12 - 20:14 
QuestionGreat article and a question for youmemberRahul.RK23 Aug '12 - 20:13 
AnswerRe: Great article and a question for youmemberashutosh k. shukla24 Aug '12 - 13:55 
GeneralRe: Great article and a question for youmemberRahul.RK28 Aug '12 - 2:04 
GeneralMy vote of 5memberMember 821240417 Aug '12 - 1:35 
GeneralRe: My vote of 5memberashutosh k. shukla18 Aug '12 - 22:10 
GeneralMy vote of 5membersuhelsa17 Aug '12 - 1:03 
GeneralRe: My vote of 5memberashutosh k. shukla18 Aug '12 - 22:10 
Generalwonderful! I give you a 5, although I get a BAD HTTP REQUEST error [modified]memberJanilane15 Aug '12 - 23:53 
GeneralRe: wonderful! I give you a 5!memberashutosh k. shukla16 Aug '12 - 0:16 
QuestionGreat write up ,How to add security?memberMember 930031613 Aug '12 - 9:18 
AnswerRe: Great write up ,How to add security?memberashutosh k. shukla15 Aug '12 - 16:11 
GeneralSimple & greatmemberPREMSONBABY9 Aug '12 - 22:34 

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

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