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.
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.
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,
- Add namespace System.ServiceModel.Web
- There are two methods WebGet and WebInvoke is available
- WebGet is for HTTP Get operation
- WebInvoke is for otherHTTP operations.
- Make sure in braces the parameter is same as parameter of the function. In this case it is name
- GetGreetingAsFunction (string name) will be called by giving function name in the URI
- 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
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
Conclusion
In this article, I explained basic of REST service. Thanks for reading.
2 responses to “Creating REST Service step by Step (A Simplest approach)”
its superb dear…….its really helpful yaar.Keep it up!!
It’s greate,
I am new for REST service,
plz tell me one simple Example perform all DML operations in DATABASE using WCFREST service how to create it,
And also How to consume this service in ASP.NET webpages,
plz give EXAMPLE
Send me Source code to my mail id:mandla.anilbabu@gmail.com
Thank you,
anil