Click here to Skip to main content
14,841,473 members
Articles / Programming Languages / C# 3.5
Posted 14 Nov 2011


149 bookmarked

Implementing 5 important principles of REST using WCF Services

Rate me:
Please Sign up or sign in to vote.
4.85/5 (55 votes)
2 Feb 2014CPOL7 min read
This article will explain about 5 important principles of REST using WCF Services.

Article updated with video on REST.

Table of Contents

What is REST?

REST is nothing but using the current features of the “Web” in a simple and effective way. If you see, some of the amazing features of the Web are:

  • 40 years old matured and widely accepted HTTP protocol.
  • Standard and Unified methods like POST, GET, PUT and DELETE.
  • Stateless nature of HTTP protocol.
  • Easy to use URI (Uniform resource identifier) format to locate any web resource.

REST leverages these amazing features of the web with some constraints.

Image 1

Let’s define REST in official words, REST is an architectural style built on certain principles using the current “Web” fundamentals.

There are 5 basic fundamentals of web which are leveraged to create REST services.

Principle 1: Everything is a Resource

Internet is all about getting data. This data can be in a format of web page, image, video, file, etc. It can also be a dynamic output like get customers who are newly subscribed. The first important point in REST is start thinking in terms of resources rather than physical files.

Image 2

Below are some examples of resources with URI:

So the first thing is, visualize everything as resource.

Principle 2: Every Resource is Identified by a Unique Identifier

The old web identifies all resources by a URI. For instance, if you want to display customer with their orders, you can use In REST, we add one more constraint to the current URI, every URI should uniquely represent every RESOURCE data.

For instance, you can see the below unique URI format for customer and orders fetched.

Customer dataURI
Get Customer details with name “Shiv”
Get Customer details with name “Raju”
Get orders placed by customer "Shiv"
Get orders placed by customer “Raju”

Principle 3: Use Simple and Uniform Interfaces

Simplification is the way to success and that’s what exactly the goal of uniform interfaces is. When external clients are interacting with web resources, they expect simplified interfaces.

For instance, let’s say you have exposed customer and orders data / resource on the web. Let’s assume below are the methods / function names by which external clients can communicate to your application.

  • AddCustomer
  • InsertOrders
  • SelectCustomer
  • getOrders
  • DeleteCustomer
  • RemoveOrders
  • UpdateCustomers

Do the above method names look inconsistent and difficult to remember? Yes they do. Now what REST says is keep your interfaces uniform and simple. This can be achieved by using the uniform methods of HTTP protocol and combining the same with your resource operation.

Below is the list of HTTP methods for getting, creating, updating and deleting a resource on the web.

GETGet a resource
PUTCreate and Update a resource
DELETEDeletes a resource
POSTSubmits data the resource

Image 3

Now by combining the standard HTTP methods and your resource names, you can have uniform interfaces and thus leading to simplified communication. You can see from the below table how we have created uniform REST URL using standard HTTP methods.

Normal Method namesHTTP methodsREST Uniform URL

Image 4

Principle 4: Communication is Done by Representation

When you send any request or you get any response, you are actually sending representations.

Image 5

For example, let’s say you want to create a new customer record, you would send some kind of representation as shown below using HTTP PUT.

   <Address>Mulund Mumbai</Address>

Once the resource is created, you would get a representation as shown below. The below representation says the customer questpond has been successfully created and if you wish, you can use “” defined in the “Next” tag to create orders for the “questpondcustomer.


The above representation was in XML format. If you wish, you can also send and receive other presentations like JSON. For instance, below is a simple JSON snippet for creating a new customer record with name and address.

{Customer :{ Name:'', Address:'Mulund Mumbai'}}

Principle 5: Be Stateless

Every request should be an independent request so that we can scale up using load balancing techniques. Independent request means with the data also send the state of the request so that the server can carry forward the same from that level to the next level.

For instance, below are two simple representations, the first representation is sent for logging in.


If you are logged in successfully you receive the below representation.


If you want to search for customer, you would send him the below representation stating that you are in a state of successful logging in and would like to search all customers.


In other words, every request is independent and the server does not need to remember your previous request and states.

Summarizing 5 Principles of REST

So summarizing REST leverages the old web HTTP by applying principles 5 important principles:

  • Everything is a resource.
  • Every resource is identified by a unique identifier.
  • Use simple and uniform interfaces.
  • Communication are done by representation.
  • Be Stateless.

Image 6

Implementing REST with WCF Services

Enabling WCF REST services is a three step process:

  • Enable HTTP bindings
  • Expose methods via common interfaces
  • Define your representation

In order to enable HTTP bindings, the first thing is to create an end point behavior with “webHttp” binding. The “webHttp” binding enables HTTP protocol on your WCF services.

        <behavior name="NewBehavior0">
          <webHttp />

Create a new endpoint and bind the behavior with your endpoint.

<service name="WcfRest.Service1">

<endpoint behaviorConfiguration="NewBehavior0" binding="webHttpBinding"
bindingConfiguration="" contract="WcfRest.IService1" />


In order to make your interfaces uniform, you can use the “UriTemplate” property as shown in the below figure. You can see from the below code snippet how “getStudents” can now be called by using a http GET request on “/Students/123”, where 123 is the id of the student.

[OperationContract][WebGet(UriTemplate = "/Students/{id}")]string getStudents(string id);

In the same way, in the below code snippet, we are calling “AddStudent” method using HTTP POST and we have made the interface uniform using the “UriTemplate” property as shown in the below code snippet.

[OperationContract]WebInvoke(Method = "POST", 
    UriTemplate = "/Students/{studentname}")]bool Addstudent(string studentName);

So now, if you want to get a particular student use http://localhost:27683/Students.svc/Students/1 with HTTP GET method and if you want to add a new student call http://localhost:27683/Students.svc/Students/Shiv with HTTP POST. Isn’t that much simpler as compared to “getStudents” and “AddStudent” methods.

Image 7


public interface IStudents

[WebGet(UriTemplate = "/Students/{id}")]        
string getStudents(string id);

[WebInvoke(Method = "POST", UriTemplate = "/Students/{studentname}")]        
bool Addstudent(string studentName);     


Below is the implemented code for the above interface. We have created a collection of student names in a string collection. In order to add new students, we can call “AddStudent” and to retrieve students, we can call “getStudents”.

public class Students : IStudents    
List<string> ostudents = new List<string>();        
public  Students()        
public string getStudents(string id)        
return ostudents[0];        
public bool Addstudent(string StudentName)        
return true;        

Testing the Above WCF REST Services using Simple HTML Form

To get a particular student, we need to invoke “getStudents” by using HTTP GET on the URL http://localhost:27683/Students.svc/Students/1 . The “1” indicates that we want to retrieve the student with ID “1”.

To add a particular student, we need to invoke “Addstudent“ by using HTTP POST on the URL http://localhost:27683/Students.svc/Students/Shiv. “Shiv” is the student name we wish to add to the collection.

In order to make a post and get on the above URLs, we will use simple HTML to do the same. You can also use other methods like “xmlhttprequest”, WCF proxy, etc. In order to keep it simple in this article, we will use simple HTML for now.

 <form action="http://localhost:27683/Students.svc/Students/1" method="get">
<input type="submit" />

So if you run the above HTML with the action pointing to the URL http://localhost:27683/Students.svc/Students/1 and using the “get” method, you should get the below output.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<string xmlns="">Shivprasad</string> 

In the same way, if you run the below HTML with the action pointing to the URL (http://localhost:27683/Students.svc/Students/shiv) and using the “post” method, you should get the below output.

<form action="http://localhost:27683/Students.svc/Students/shiv" method="post">
  <input type="submit" />

This XML file does not appear to have any style information associated with it. The document tree is shown below:

<boolean xmlns="">true</boolean> 

In the same way, you can try for other HTTP methods like PUT, DELETE, etc.

What about Representations?

When we discussed REST fundamentals, we said one of the most important things is representation and format of representation. The current WCF [WebGet] and [WebInvoke] attributes help you to configure formats in both request and response in XML and JSON format.

In the below code snippet, we have created the Responseformat as JSON.

[OperationContract] [WebGet(ResponseFormat=WebMessageFormat.Json, 
    UriTemplate = "/Students/{id}?format=json")]string getStudentsJSON(string id);

If you do a get on the URL you should get the value as shown in the below figure: -

Image 8

Video on REST

If you are not clear with the above concepts explained, see our video on REST: -

Image 9

Do watch my 500 videos on C#, .NET, WCF, Design Patterns, SilverLight, SharePoint, etc.


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


About the Author

Comments and Discussions

SuggestionSection@Principle 3: Use Simple and Uniform Interfaces Pin
chetan.gupta217-Feb-17 2:38
Memberchetan.gupta217-Feb-17 2:38 
QuestionThanks for video Pin
Pranay Rana3-Feb-14 3:51
professionalPranay Rana3-Feb-14 3:51 
QuestionBroken Links Pin
lw@zi 17-Dec-13 18:49
professional lw@zi 17-Dec-13 18:49 
QuestionWCF Rest + ADo.Net (.edmx) Entity Model Pin
ravithejag16-Oct-13 23:56
Memberravithejag16-Oct-13 23:56 
GeneralMy vote of 2 Pin
Member 1028117626-Sep-13 0:21
MemberMember 1028117626-Sep-13 0:21 
GeneralMy vote of 5 Pin
Guih Martins24-Sep-13 2:45
MemberGuih Martins24-Sep-13 2:45 
QuestionHow to implement RESTful services using Jquery/javascript Pin
mt40619-Feb-13 1:17
Membermt40619-Feb-13 1:17 
QuestionHow to implement Basic Authentication Pin
Sunasara Imdadhusen20-Jul-12 1:44
professionalSunasara Imdadhusen20-Jul-12 1:44 
GeneralMy vote of 1 Pin
Grant Parks11-Apr-12 17:56
MemberGrant Parks11-Apr-12 17:56 
GeneralMy vote of 5 Pin
osedok12-Feb-12 15:15
Memberosedok12-Feb-12 15:15 
GeneralMy vote of 5 Pin
ronzulu9-Dec-11 13:01
Memberronzulu9-Dec-11 13:01 
GeneralMy vote of 5 Pin
Gaston Verelst29-Nov-11 1:01
MemberGaston Verelst29-Nov-11 1:01 
GeneralMy vote of 5 Pin
albertobr21-Nov-11 5:30
Memberalbertobr21-Nov-11 5:30 
GeneralRe: My vote of 5 Pin
Shivprasad koirala21-Nov-11 6:34
MemberShivprasad koirala21-Nov-11 6:34 
GeneralRe: My vote of 5 Pin
albertobr21-Nov-11 6:42
Memberalbertobr21-Nov-11 6:42 
GeneralMy vote of 5 Pin
Nickos_me16-Nov-11 1:09
MemberNickos_me16-Nov-11 1:09 
QuestionI like, but Pin
Sean Hederman16-Nov-11 0:33
professionalSean Hederman16-Nov-11 0:33 
AnswerRe: I like, but Pin
Shivprasad koirala16-Nov-11 0:59
MemberShivprasad koirala16-Nov-11 0:59 
GeneralMy vote of 5 Pin
Reiss15-Nov-11 1:09
professionalReiss15-Nov-11 1:09 
QuestionGood Stuff Pin
Dave Kerr14-Nov-11 23:42
mentorDave Kerr14-Nov-11 23:42 
AnswerRe: Good Stuff Pin
Umesh Bhosale8-Jan-14 0:08
MemberUmesh Bhosale8-Jan-14 0:08 

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.