Creating WCF REST Service

Background
 
I have read many forum posts regarding WCF REST service creation and implementation but it's difficult to understand for beginners, so to make it understandable, decided to write this article which shows step by step how to create WCF REST Service.
 
Prerequisites
 
To understand WCF REST service, you need to have at least beginner knowledge on WCF. If you are a beginner in WCF then please refer my following articles on C# Corner for WCF.
  1. Introduction To WCF Endpoints
  2. Introduction to WCF Services
  3. Creating WCF Service
I hope you have read the preceding articles, now let us start with WCF REST from the definition.
 

What is WCF REST?

 
REST stands for Representational state transfer which is a technique to communicate on cross-platform application and exchange the data in JSON or XML format with the help of GET, POST, PUT, and DELETE methods of HTTP protocol.
 
Let us briefly understand about the HTTP methods which is most commonly used to create WCF REST service:
  • GET : Get the resource (Records) from particular source such as SQL database.
  • POST: Used to insert the records into the particular source such as SQL, Oracle database.
  • PUT : Used to modify the resource or records.
  • DELETE : Used to delete the specific resource or record from a particular source.
I hope you understood the basics about REST concept. Now let us start creating WCF REST Service through a step by step approach.
 

Step 1: Create WCF Service

 
To know how to create WCF service in-depth, please refer my article creating WCF Service (link mentioned above). So, let's see a simple way to create WCF service:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2015".

  2. "File" - "New Project" - "C#" - WCF Service Application as in the following screenshot:

    WCF Service Application

  3. Provide the project name such as "PayMentRESTService " or another as you wish and specify the location.

  4. Now delete the auto created Interface and svc file which we will create a new one so beginners can understand it.

    delete the auto created Interface

  5. Now Add New WCF Service file and give name PayMentRESTService as in the following screenshot:

    Add New WCF Service file
I hope you have followed the same steps and learned how to add WCF Service. After adding the Service file, the project solution explorer will be as in the following screenshot:
 
solution explorer  
 

Step 2: Configure REST Service Template

 
Now open the IPaymentService.cs Interface file and write the following code:
  1. [ServiceContract]    
  2. public interface IPayMentService    
  3. {    
  4.     [OperationContract]    
  5.     [WebInvoke(Method = "GET",UriTemplate = "/PayBill/{PayId}", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]    
  6.     string PayBill(string PayId);    
  7.        
  8. }   
Let us understand above REST Template using the following diagram
 
Code 
 
I hope you have understood the basic REST Template from the above image.
 

Step 3: Implement IPaymentService.cs interface methods into PaymentService.svc.cs file as.

  1. public class PayMentService : IPayMentService    
  2. {    
  3.     public string PayBill(string PayId)    
  4.     {    
  5.         return "Transaction having PayId " + PayId + " was successful";    
  6.     }    
  7. }  
Now our REST Service Code is ready. Let us complete the other steps.
 

Step 4: Configure End Points and Service Behaviors in web.config file

 
End Points and Service Behaviors configuration is very important in WCF Service. Many people say that it is complicated to configure, but trust me its much easier and simple with powerful intellisense. Let's open web.config file and find system.serviceModel tag and here are the steps:
 
Configure service behaviors as:
 
Configure service behaviors 
 
Configure End points as:
 
Configure End points 
 
While configuring Endpoints Tag automatically shows how to set and what contract because it shows the list of contract files (Interfaces) i.e. service contract. I hope you got a basic idea about the End points configuration. Soon I will post a video on this. After configuring Endpoints and service behaviors the system.serviceModel tag section of web.config file will be as in the following code snippet:
  1. <system.serviceModel>    
  2.     <behaviors>    
  3.       <serviceBehaviors >    
  4.         <behavior name="ServiceBehavior">    
  5.           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->    
  6.           <serviceMetadata httpGetEnabled="true"/>    
  7.           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->    
  8.           <serviceDebug includeExceptionDetailInFaults="false"/>    
  9.         </behavior>    
  10.       </serviceBehaviors> 
  11.       <endpointBehaviors>    
  12.         <behavior name="web"
  13.           <webHttp/>
  14.         </behavior>
  15.       </endpointBehaviors>
  16.     </behaviors>    
  17.     <services>    
  18.       <service name="PayMentRESTService.PayMentService" behaviorConfiguration="ServiceBehavior">
  19.         <endpoint binding="webHttpBinding" contract="PayMentRESTService.IPayMentService" behaviorConfiguration="web">
  20.         </endpoint>    
  21.       </service>
  22.     </services>
  23.     <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />    
  24.   </system.serviceModel>    
I hope you have done same configuration settings which I have done .
 

Step 5: Test REST Service

 
Now our service is ready. Let's test it using REST client of mozilla browser as in the following:
 
Our REST Service URL will be http://localhost:64858/PayMentService.svc/PayBill/100
 
Test REST Service 
 
In the above Response of WCF REST service you have seen that the status code is 200 OK. It means our service executed successfully. Let's confirm by switching to Response Body tab of REST client as in the following screenshot:
 
responce 
 
From the preceding output its clear that our service executed successfully and as per configuration it returned JSON output. I hope from the preceding examples we have learned how to create WCF REST Service.
 
Note:
  • Configure the database connection in the web.config file depending on your database server location.
  • Download the Zip file of the sample application for a better understanding.
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
Summary
 
I hope this article is useful for all readers. If you have any suggestions, then please mention it in the comments section.


Similar Articles