Click here to Skip to main content
14,304,866 members

Create REST Service using WCF

Rate this:
2.86 (10 votes)
Please Sign up or sign in to vote.
2.86 (10 votes)
6 Jun 2011CPOL
Create simple REST service using WCF and VS 2010


REST stands for Representational State Transfer. Central components are resources and actions which can affect those resources. This article is the same as my original post here.

REST Actions

GET:      GET is used exclusively to retrieve data.
DELETE:   DELETE is used for deleting resources.
PUT:      PUT is used to add or change a resource.
POST:     POST is Used to modify and update a resource 


In WCF, the most important elements of a REST application include the [WebGet] and [WebInvoke] attributes and the new binding webHttpBinding in connection with the webHttp endpoint behavior.

WebHttpBehavior class or the endpoint behavior also enables you to choose from two different serialization formats: 

  • POX (Plain old XML) uses XML alone without the SOAP overhead.
  • JSON (JavaScript Object Notation) is a very compact and efficient format, primarily in connection with JavaScript.


The following examples show how [WebGet] and [WebInvoke] are used.

STEP 1: Define Data Contract

public class Student
    public string StudentName { get; set; }
    public int Age { get; set; }
    public double Mark { get; set; }

Step 2: Define Service Contract

public interface IStudentService
    [WebGet(UriTemplate = "/GetStudentObj")]
    Student GetStudent();
    [WebInvoke(UriTemplate = "/DeleteStudent/{studentName}",
    Method = "DELETE")]
    void DeleteStudent(string studentName);

Step 3: Implement Service Contract

public class StudentService : 
    public Student GetStudent()
        Student stdObj = new Student { 
                    StudentName = "Foo", 
                    Age = 29, 
                    Mark = 95 };
        return stdObj;

    void DeleteStudent(string studentName)
        // delete logic

Step 4: Use webHttpBinding (change default http port mapping to webHttpBinding)  

		<add scheme="http" binding="webHttpBinding"/>


Step 5: Specify webHttp End Point Behaviors

                <webHttp />
            </behavior >

Step 6: Test the Service 



This tutorial was created to help you understand the WCF REST services. I hope you have found this tutorial helpful and interesting.


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


About the Author

A Mahesh
Web Developer
India India
Symphony Services, Banglore, 560 087, India

Comments and Discussions

GeneralHmacSha256Encode by Pin
abdel moneim15-Nov-18 7:35
memberabdel moneim15-Nov-18 7:35 
GeneralMy vote of 3 Pin
vsrikanth874-Apr-14 1:49
membervsrikanth874-Apr-14 1:49 
Questionwhy endpointbehavior is required for webhttpbinding Pin
Milan Raval18-Feb-14 21:31
memberMilan Raval18-Feb-14 21:31 
QuestionHow to create WCFREST service in .net perform all CRUD operations Pin
Member 917112823-Jul-12 23:36
memberMember 917112823-Jul-12 23:36 
QuestionIt is not working Pin
vadim3076-Jun-12 3:41
membervadim3076-Jun-12 3:41 
GeneralMy vote of 1 Pin
davefrey1214-Jun-11 7:23
memberdavefrey1214-Jun-11 7:23 
GeneralMy vote of 1 Pin
fjdiewornncalwe8-Jun-11 2:50
professionalfjdiewornncalwe8-Jun-11 2:50 
General[My vote of 1] Needs considerable work Pin
Richard MacCutchan8-Jun-11 0:59
protectorRichard MacCutchan8-Jun-11 0:59 
GeneralMy vote of 1 Pin
Selvin7-Jun-11 3:32
memberSelvin7-Jun-11 3:32 

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.

Posted 6 Jun 2011


25 bookmarked