Creating RESTful Services Using WCF

REST stands for the Representational State Transfer, which is an architectural style for networked hypermedia applications. It is primarily used to create the Service, which is lightweight, maintainable and scalable. A Service based on REST is called RESTful Service.

This article explains how to create RESTful Service, using WCF (Windows Communication Foundation).This can be accomplished in the steps given below.

Create WCF Project

  1. Open Visual Studio.
  2. Add New ASP.NET Web Application.

    Visual Studio

  3. Choose Empty and select Web Form, if you are using Visual Studio 2015. 

    Visual Studio

    Now, we are ready with our project and let's add the Service named “Cricket”.

Adding Datacontract

Thus, we are ready with our Service, which will do our work for us. Now, the next step is to add the Datacontract, which will be used to pass the data to and from the Services .

  1. [DataContract]  
  2. publicclassCricketer {  
  3.     [DataMember]  
  4.     publicstring Name {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     [DataMember]  
  9.     publicstring Team {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     [DataMember]  
  14.     publicstring ODI {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     [DataMember]  
  19.     publicstring Test {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  
Don’t forget to mark the class as DataContract, as it defines what data needs to be exchanged, using serialization engine called Data Contract Serializer. Also, the DataMember attribute will help to serialize the property.

Thus, we are ready with the DataContractClass. Lets see what changes are required to make to the ServiceContract.

ServiceContract

The main part of the RESTful Service is the Service contract part that is our interface, which has defined Operation contract. Our Service contract is given below. 
  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Runtime.Serialization;  
  5. usingSystem.ServiceModel;  
  6. usingSystem.Text;  
  7. usingSystem.ServiceModel.Web;  
  8. usingWCFRestServices;  
  9. namespaceWCFRestServices {  
  10.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICricket" in both code and config file together.  
  11.     [ServiceContract]  
  12.     publicinterfaceICricket {  
  13.         [OperationContract]  
  14.         [WebInvoke(UriTemplate = "/GetCricketer", Method = "Get", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  15.         CricketerGetCricketerDetails();  
  16.         [OperationContract]  
  17.         [WebInvoke(UriTemplate = "/AddCricketer", Method = "Post", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  18.         intAddCricketer(CricketerobjCricketer);  
  19.     }  
  20. }  
Visual Studio

Now, coming to the OperationContract, we can see one more attribute is added. WebInvokeit has few parameters, which are as follows.
  • Method
    Gets and sets the protocol (for example HTTP) method the Service operation responds to (Get, Post, Put, Delete etc.)

  • RequestFormat
    Get or set the format of the request and it can be JSON or XML.

  • ResponseFormat
    Get or set the format of the response and it can be JSON or XML.

  • UriTemplate
    It is the Uniform Resource Identifier(URI) for the Service operation.

Configuring Service and Behaviors in Web.Config

  1. <system.serviceModel>  
  2.     <services>  
  3.         <servicename="WCFRestServices.Cricket" behaviorConfiguration="servicebehavior">  
  4.             <endpointaddress="" binding="webHttpBinding" contract="WCFRestServices.ICricket" behaviorConfiguration="web">  
  5.                 </endpoint>  
  6.                 </service>  
  7.     </services>  
  8.     <behaviors>  
  9.         <serviceBehaviors>  
  10.             <behaviorname="servicebehavior">  
  11.                 <serviceMetadatahttpGetEnabled="true" />  
  12.                 <serviceDebugincludeExceptionDetailInFaults="true" /> </behavior>  
  13.         </serviceBehaviors>  
  14.         <endpointBehaviors>  
  15.             <behaviorname="web">  
  16.                 <webHttp/> </behavior>  
  17.         </endpointBehaviors>  
  18.     </behaviors>  
  19.     <serviceHostingEnvironmentmultipleSiteBindingsEnabled="true">  
  20.         </serviceHostingEnvironment>  
  21. </system.serviceModel>  
Here, the webconfigchanges for the Service is the most important part. In it, Binding is used.

It is “webHttpBinding”. This binding is used for RESTful Services in WCF.

I hope you understood the basics of creating the RESTful Services. WCF data operations are not included in this article as the main focus is on the Service part. We can create the database interaction, using ADO.NET, Entity Framework or any other data access technology.

Happy coding.