WCF REST service – part 1

REST (Representational State Transfer) has emerged in the last few years alone as a predominant Web service design model. In fact, REST has had such a large impact on the Web that it has mostly displaced SOAP- and WSDL-based interface design because it’s a considerably simpler style to use.
But i am not going to talk about SOAP Vs REST. This post only describes how to implement REST web services usinf WCF model. Again WCF is just another way of implement REST web service.

Whenever we consume a WCF service from a client, we need to create a proxy and need to import all the types(xsd) to your client. Well, nothing wrong with this but think about a scenario if we can use all the web methods as they were a resource on a remote machine. REST will enable us to consume our web methods with good old Http verbs (GET,POST,PUT,DELETE).
Enough discussion right? Lets start some code 🙂

Create WCF service
Data Contract

 <code> [DataContract]
 public partial class Employee
 public string Name { get; set; }</code>

 public string EmployeeID { get; set; }

 public string Email { get; set; }

Service contract Interface

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

using System.ServiceModel.Activation;
 public interface IEmployeeService
 [WebGet(UriTemplate = "/GetEmployees", ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json )]
 Employee[] GetEmployees();</code>

 [WebInvoke(Method = "POST", UriTemplate = "/GetEmployeeDetails/{EmployeeID}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
 Employee GetEmployeeDetails(string EmployeeID);

 [WebInvoke(UriTemplate = "/AddEmployee", Method = "POST",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
 Employee AddEmployee(Employee emp);

 [WebInvoke(UriTemplate = "/DeleteEmployee", Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
 bool DeleteEmployee(string EmployeeID);

1. UriTemplate is the url format which a client will request. lets say our service is accessed at “http:localhost:3839/Service1.svc” then “http:localhost:3839/Service1.svc/GetEmployees” will call the GetEmployees method which returns Employee[].

2. ResponseFormat/RequestFormat is the data format which is used to send/receive the data to/from server. JSON and XML are two formats supported by WCF.

3. WebGet and WebInvoke WebGet is used for GET requests and WebInvoke can specify any http verb with Method=”GET|POST|DELETE|PUT”.

Service Implementation

 <code>[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
 public class EmployeeService : IEmployeeService
 private List employees = null;
 #region IEmployeeService Members
 public Employee[] GetEmployees()
 employees = new List();
 employees.Add(new Employee() { EmployeeID = "1", Email = "test@test.com", Name = "Praveen" });
 employees.Add(new Employee() { EmployeeID = "2", Email = "test@test.com", Name = "Prashant" });
 employees.Add(new Employee() { EmployeeID = "3", Email = "test@test.com", Name = "Ritu" });
 return employees.ToArray();

public Employee GetEmployeeDetails(string EmployeeID)
 return new Employee() { EmployeeID = EmployeeID, Email = "tdfgdgest@test.com", Name = "sfsdgg" };

public Employee AddEmployee(Employee emp)
 return emp;

public bool DeleteEmployee(string EmployeeID)
 // delete in DB
 return true;

Note: You can replace these methods with something significant code 🙂 here it is just sample.

Web.config settings

 <services >
 <service name="WCFSamples.EmployeeService" behaviorConfiguration="restBehavior" >
 <endpoint address=""
 behaviorConfiguration="rest" >
 <identity >
 <dns value="localhost" />


 <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
 <behavior name="rest">
 <webHttp />
 <behavior name="restBehavior">
 <serviceMetadata httpGetEnabled="true" />
 <serviceDebug includeExceptionDetailInFaults="false" />
 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

Note: For REST service Binding is always webHttpBinding also endpointbehavior also must be added like below.

<behavior name="rest">
<webHttp />

.Svc file markup 


<%@ ServiceHost Language="C#" Debug="true" Service="WCFSamples.EmployeeService" CodeBehind="Service1.svc.cs" %>

This is all we need to know about creating a WCF REST enabled service.
Next post will describe how to consume it in different ways.

Hope it helps!!


About saxenapraveen

I have 14 years of experience in enterprise software development,designing and leading team to provide reliable and scalable solutions for cloud and device ready businesses.

Posted on February 14, 2012, in .NET, WCF. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: