Click here to Skip to main content
11,438,069 members (50,105 online)
Click here to Skip to main content

Create REST Service using WCF

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

Introduction

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 

WCF & REST

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.

Demo

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

STEP 1: Define Data Contract

[DataContract]
public class Student
{
    [DataMember]
    public string StudentName { get; set; }
    [DataMember]
    public int Age { get; set; }
    [DataMember]
    public double Mark { get; set; }
} 

Step 2: Define Service Contract

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

Step 3: Implement Service Contract

public class StudentService : 
            IStudentService
{
    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)  

<system.serviceModel>
	<protocolMapping>
		<add scheme="http" binding="webHttpBinding"/>
	</protocolMapping>
	<behaviors>

<system.serviceModel>

Step 5: Specify webHttp End Point Behaviors

<system.serviceModel>
    -----
    </protocolMapping>
    <behaviors>
        <endpointBehaviors>
            <behavior>
                <webHttp />
            </behavior >
        </endpointBehaviors>
    <behaviors>
	------
<system.serviceModel> 

Step 6: Test the Service 

http://localhost/StudentService.svc/GetStudentObj      
http://localhost/StudentService.svc/DeleteStudent/foo 

Conclusion

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

License

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

Share

About the Author

A Mahesh
Web Developer
India India
Symphony Services, Banglore, 560 087, India
Follow on   Twitter

Comments and Discussions

 
General[My vote of 1] Needs considerable work Pin
Richard MacCutchan8-Jun-11 1:59
mvpRichard MacCutchan8-Jun-11 1:59 

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 | Terms of Use | Mobile
Web03 | 2.8.150506.1 | Last Updated 7 Jun 2011
Article Copyright 2011 by A Mahesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid