CRUD Operations In WCF REST With JSON Message Format

Sometimes we need to work with JSON data in WCF REST services to make data available across different platforms. So let us learn in this article how to make CRUD  operations in WCF REST service and communicate using JSON data. To Perform CRUD operations in WCF REST service we need to use the following HTTP methods:
  • 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 Delete the specific resource or record from particular source.
To work with specific message format in WCF REST service, we need to set Web Message Format to JSON or XML in REST template. Now let's implement above methods to make CRUD operations in WCF REST Service Step by step.
Step 1: Create WCF Service.
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2015".

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


  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 new one so beginners can understand it. 


  5. Now Add New WCF Service file and give name PayMentRESTService as: 

    I hope you have followed the same steps and and learned how to add WCF Service. After adding Service file then the project solution explorer will look like the following:

Step 2: Configure REST Service Template.
Now open the IPaymentService.cs Interface file and write the following code for CRUD operation:
  1. using System.ServiceModel;  
  2. using System.ServiceModel.Web;  
  3. namespace PayMentRESTService  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IPayMentService  
  7.     {  
  8.         //To Insert or POST Records  
  9.         [OperationContract]  
  10.      [WebInvoke(Method = "POST",
  11.       UriTemplate = "/AddPayee/{Name/{City}",    BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  12.         void AddPayee(string Name, string City);  
  14.         //To Get Records from database   
  15.         [OperationContract]  
  16.         [WebInvoke(Method = "GET",
  17. UriTemplate = "/PayBill/{PayId}",
  18. BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]  
  19.         string PayBill(string PayId);  
  21.         //To Update records  
  22.         [OperationContract]  
  23.         [WebInvoke(Method = "PUT",
  24. UriTemplate = "/UpdateBillPayment/{PayId}/{TransId}",
  25. BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  26.         void UpdateBillPayment(string PayId,string TransId);  
  28.         //To delete records  
  29.         [OperationContract]  
  30.         [WebInvoke(Method = "DELETE",
  31. UriTemplate = "/RemovePayee/{Id}",
  32. BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  33.         void RemovePayee(string Id);  
  35.     }  

 Let's understand code including REST Template.
  • IPayMentService: REST Service interface name
  • Method: HTTP methods types it can be GET,POST,PUT,DELETE and other
  • UriTemplate: To Define url structure that how can be serice method accessed at client.
  • BodyStyle: Allows to define message body style format such as Bare, Wrapped etc.
  • RequestFormat: Defines in which message format does request come from client such xml or json.
  • ResponseFormat: Defines what message format does service return to the client as a part of response such as xml or json.
I hope you have got some idea about REST Template.
Step 3: Implement IPaymentService.cs interface methods into PaymentService.svc.cs file as.
  1. public class PayMentService : IPayMentService    
  2. {    
  3.     public void AddPayee(string Name, string City)    
  4.     {    
  5.        //write database related insert logic here.    
  6.     }    
  7.     public string PayBill(string PayId)    
  8.     {    
  9.         return "Transaction having PayId " + PayId + " is successful";    
  10.         //write database related data retrival logic here.    
  11.     }    
  13.     public void RemovePayee(string Id)    
  14.     {    
  15.         //write database related delete logic here.    
  16.     }    
  18.     public void UpdateBillPayment(string PayId, string TransId)    
  19.     {    
  20.         //write database related update logic here.    
  21.     }    
  22. }  
Now our REST Service Code is ready for CRUD operation with JSON Data. Let's complete other few steps.
Step 4: Configure End Points and Service Behaviors in web.config file as:
End Points and Service Behaviors configuration is very important in WCF Service. Many people saying that it is much 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 to follow:
Configure service behaviors as:
Configure End points as:
While configuring Endpoints Tag automatically show how to set and what contract, since it shows list of contract files (Interfaces) i.e. service contract.
I hope you got the basic idea about the End points configuration.
After configuring Endpoints and service behaviors the system.serviceModel tag section of web.config file will look like the following:
  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>  
  12.       <endpointBehaviors>  
  13.         <behavior name="web">  
  15.           <webHttp/>  
  17.         </behavior>  
  19.       </endpointBehaviors>  
  21.     </behaviors>  
  22.     <services>  
  23.       <service name="PayMentRESTService.PayMentService" behaviorConfiguration="ServiceBehavior">  
  25.         <endpoint binding="webHttpBinding" contract="PayMentRESTService.IPayMentService" behaviorConfiguration="web">  
  28.         </endpoint>  
  29.       </service>  
  31.     </services>  
  33.     <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />  
  34.   </system.serviceModel> 
I hope you have done same configuration which i have done.
Step 5: Test REST Service.
Now our service is ready. Let's test it using REST client of browser extension as:
POST ( To insert records)
REST Service URL will be:
http://localhost:64858/PayMentService.svc/AddPayee/vithal Wadje/Latur
In the above url, the last two parameters are input parameters. Now test with REST client as:
Using GET Method
Using PUT Method
Using DELETE Method
Hope from preceding examples we have learned how to Implement CRUD operations in WCF REST Service.
  • 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.
  • CRUD Stands for create, read, update and delete.
To learn the basics about the WCF Services please refer my previous articles
I hope this article is useful for all the readers. If you have any suggestion, then please contact me.