Introduction To WCF RESTFulService

RESTful services are those which follow Representational State Transfer architecture style. WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc.

Introduction to RESTFulService

RESTful services are those which follow Representational State Transfer architecture style. WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that “Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls”.

RESTful architecture uses HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).It’s simple as well as lightweight. For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. Product data) in XML format.

Following are 5 simple steps to create your first RESTful service that returns data in XML.

  • Create a WCF Service Project.
  • Preparing the data (e.g. Product) to return
  • Creating Service Contract
  • Implementing Service
  • Configure Service and Behavior

Now lets create the service.

  • Open Visual Studio.
  • File - New Project - new WCF Service Application.
  • Now we will create entity which needs to be share. Add Employee.cs
  1. [DataContract]  
  2. public class Employee {  
  3.     [DataMember]  
  4.     public int EmpId {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     [DataMember]  
  9.     public string Name {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     [DataMember]  
  14.     public string DepartmentName {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     [DataMember]  
  19.     public int Age {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  

Now add one more class which will return the collection of employees

  1. public partial class Employees {  
  2.     // private static readonly Employee _emp = new Employee();  
  3.     // private Employees();  
  4.     public static readonly Employees _emps = new Employees();  
  5.     public static Employees Instance {  
  6.         get {  
  7.             return _emps;  
  8.         }  
  9.     }  
  10.     public List < Employee > emps {  
  11.         get {  
  12.             return empList;  
  13.         }  
  14.     }  
  15.     private List < Employee > empList = new List < Employee > () {  
  16.         new Employee() {  
  17.                 Name = "e1", DepartmentName = "D1", Age = 25  
  18.             },  
  19.             new Employee() {  
  20.                 Name = "e2", DepartmentName = "D2", Age = 35  
  21.             },  
  22.             new Employee() {  
  23.                 Name = "e3", DepartmentName = "D3", Age = 45  
  24.             },  
  25.             new Employee() {  
  26.                 Name = "e4", DepartmentName = "D4", Age = 26  
  27.             },  
  28.     };  
  29. }  

Now Add Service i.e. EmployeeRESTService.svc [ServiceContract]

  1. public interface IEmployeeRESTService {  
  2.     [OperationContract]  
  3.     void DoWork();  
  4.     [OperationContract]  
  5.     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetEmployeeLsit/")]  
  6.     List < Employee > GetEmployeeLsit();  
  7. }  
  8. public class EmployeeRESTService: IEmployeeRESTService {  
  9.     void IEmployeeRESTService.DoWork() {  
  10.         throw new NotImplementedException();  
  11.     }  
  12.     List < Employee > IEmployeeRESTService.GetEmployeeLsit() {  
  13.         return Employees.Instance.emps;  
  14.     }  
  15. }  

IEmployeeRESTService contains only one method i.e. GetEmployeeLsit. Important points to understand about this method are WebInvoke attribute parameters.

  • Method = “GET”, represents an HTTP GET request.
  • ResponseFormat = WebMessageFormat.Xml, response format will be XML here but we can return JSON as well by changing its value to WebMessageFormat.json.
  • BodyStyle = WebMessageBodyStyle.Bare, indicates neither the request and nor response are wrapped. Other possible values for BodyStyle are Wrapped, WrappedRequest, WrappedResponse.
  • UriTemplate = “GetEmployeeLsit /”, it has two parts, URL path and query.
Now the last step is to configure Service and Behavior
  1. <services>  
  2.     <service name="WcfService1.EmployeeRESTService" behaviorConfiguration="serviceBehaviors">  
  3.         <endpoint address="" binding="webHttpBinding" contract="WcfService1.IEmployeeRESTService" behaviorConfiguration="web"> </endpoint>  
  4.     </service>  
  5. </services>  
  6. <behaviors>  
  7.     <serviceBehaviors>  
  8.         <behavior name="serviceBehavior">  
  9.             <serviceMetadata httpGetEnabled="true" />  
  10.             <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>  
  11.     </serviceBehaviors>  
  12.     <endpointBehaviors>  
  13.         <behavior name="web">  
  14.             <webHttp/> </behavior>  
  15.     </endpointBehaviors>  
  16. </behaviors>  
  17. <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” /> </system.serviceModel>  

webHTTPBinding is the binding used for RESTful services.

Now, everything about creating RESTful service is done. You can easily run and test it.