.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
ASPEvil
Jean Paul
satyapriyanayak
Ashutosh Jha
SP

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating REST Service step by step Tutorial

Posted By:Dhananjay Kumar       Posted Date: January 18, 2010    Points: 25    Category: WCF    URL: http://www.dhananjaykumar.net  

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

Objective

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

Background

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.

Step 1


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

1.gif

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.

2.gif

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 tag.


  
  
  


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



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

Step 5:
Run the application

3.gif

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

4.gif

Conclusion


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

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend