Creating REST Service step by Step (A Simplest approach)

No.of Views1985
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:57
Tag : WCF , REST Services
This article will give a step by step visual explanation of how to create a REST enabled WCF service.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will give a step by step visual explanation of how to create a REST enabled WCF service.

Back Ground

I have written before also REST enabled service. I was success to create REST service but now I realized that was the bit complex way. To have an understanding of REST service read my other articles.

Step1

Create an empty web site. To do this, File -> New -> Web Site.

Image Loading

Step 2

Right click on the project in solution and add WCF Service as new item. Give any name to the service. I am giving Search.svc.

Image Loading

Step 3

We are going to create a very simple service. This service will return a string. 

Search.svc

public string DoWork(string Name )
{return "HI , " + Name + " Welcome to simplest REST Service";
}
public string GetGreetingAsFunction(string Name)
{return "HI , " + Name + " Welcome to simplest REST Service";
}

Constructing URI to invoke the service

[OperationContract]
[WebGet(UriTemplate="/Search?name={name}",BodyStyle=WebMessageBodyStyle.Bare)]string  GetGreeting(string name);

The above function will get call by URL

http://localhost:61131/MyRestService/Search.svc/Search?Name=Dhananjay[OperationContract]
[WebGet]
string GetGreetingAsFunction(string name);

The above function will get call by URL
http://localhost:61131/MyRestService/Search.svc/GetGreetingAsFunction?Name=Dhananjay
In this case , we have not constructed any URI so , we will call by calling the function name directly and passing the parameter appended with ? mark.

So , contract could be consolidted as 

ISearch.cs 

[ServiceContract]
public interface ISearch
{
    [OperationContract]
    [WebGet(UriTemplate = "/Search?name={name}", BodyStyle = WebMessageBodyStyle.Bare)]string  GetGreeting(string name);
    [OperationContract]
    [WebGet]string GetGreetingAsFunction(string name);
}

Few Points to be noted


1. Add namespace System.ServiceModel.Web
2. There are two methods WebGet and WebInvoke is available
3. WebGet is for HTTP Get operation
4. WebInvoke is for otherHTTP operations.
5. Make sure in braces the parameter is same as parameter of the function. In this case it is name
6. GetGreetingAsFunction (string name) will be called by giving function name in the URI
7. The above service will be available at uri

Step 4

Modifying the Config file to make service as REST enabled service

Create the End Point Behavior, add the below End Point behavior inside <Behavior> tag.

<endpointBehaviors><behavior name ="REST"><webHttp/></behavior></endpointBehaviors>

Modify the End Point setting. Give WebHttpBinding as binding. And provide behavior we created above as behavior name.

<endpoint address="" binding="webHttpBinding" contract="ISearch" behaviorConfiguration="REST">

After putting all together Web.Config wil look like 

Web.Config

<?xml version="1.0"?><configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name="SearchBehavior"><serviceMetadata httpGetEnabled="true"/><serviceDebug includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors><endpointBehaviors><behavior name="REST"><webHttp/></behavior></endpointBehaviors></behaviors><services><service behaviorConfiguration="SearchBehavior" name="Search"><endpoint address="" binding="webHttpBinding" contract="ISearch" behaviorConfiguration="REST"><identity><dns value="localhost"/></identity></endpoint><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/></service></services></system.serviceModel><system.web><compilation debug="true"/></system.web></configuration>

Step 5 :Run the application

Image Loading

Below URL are used to call the service in browser.

http://localhost:61131/MyRestService/Search.svc/Search?Name=Dhananjay

http://localhost:61131/MyRestService/Search.svc/GetGreetingAsFunction?Name=Dhananjay

Image Loading

 

Conclusion

In this article, I explained basic of REST service. Thanks for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top