Creating REST Service Step by Step (A Simplest Approach)


Objective

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

Background

I have written previously about REST enabled services. I have successfully created a REST service but now I realize that that way was a bit complicated. To have an understanding of a 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 the solution and add a WCF Service as a new item. Give any name to the service. I am using 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 be called by the URL:

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

[OperationContract]
[WebGet]

string
GetGreetingAsFunction(string name);

The above function will be called by the URL:

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

In this case, we have not constructed any URI, so we will call it by calling the function name directly and passing the parameter appended with a question mark ("?").

So the contract could be consolidated 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);

}

A 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 the service as a REST enabled service

Create the End Point Behavior, add the following End Point behavior inside <Behavior> tag:

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

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

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

After putting all that together the Web.Config will 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

3.gif

The following URLs are used to call the service in a browsr:

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 the basics of REST services. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all