CRUD Operations Using WCF

Introduction

In this article we will learn how to create a WCF REST services with CRUD operations. Let's understand what REST is. REST stands for Representational State Transfer and helps us to create, read, update or delete information on a server using simple HTTP calls. REST is a software architecture and applied to describe the web architecture, to identify existing problems, to compare alternative solutions and ensure that protocol extensions would not violate the core constraints that make the web successful. Web service APIs adhere to the REST constraints that are called RESTful. HTTP methods that are typically used to implement a RESTful API, they are the following.

  • GET : Retrieve the information
  • PUT : Replace the entire collection with another collection. 
  • POST : Create a new entry to the collection.
  • DELETE : Delete the entire collection.

The PUT and DELETE operations will produce the same result, no matter how many times it is repeated. The GET method is a safe method, it retrieves or accesses records but doesn't change them.

CRUD Operations on WCF using LINQ to SQL Classes are described below.

Select a WCF Service Application as in the following:

SelectWCF

Create a Database and use the LINQ to SQL Classes as in the following:

Right-click on the project then select "Add" then click on "Add" and select "New Items".

LinqToSqlClass

Then you will see something like this.

blankLinQToSqlImg2

Click on the Server Explorer and connect to the database.

connectToDatabase

Submit all the credentials, like username, password and server name, then choose the database.

CredentialForConnection

Now select the table and drag and drop that table.

 DragAndDropTheDb

Now add some operation contracts in our WCF service, so in this article I have added four types of methods.

operationContratc

  1. public interface IService1  
  2.     {  
  3.             
  4.         [OperationContract]  
  5.         [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Search/")]    
  6.        List<BloodApp> Search(string bloodGrp, string pin);  
  7.           
  8.         [OperationContract]  
  9.         [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/ShowAll/")]    
  10.         List<BloodApp> ShowAll();  
  11.    
  12.         [OperationContract]  
  13.         [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Insert/")]    
  14.         void Insert(string name, string age, string gendr, string bldgrp, string Mob, bool Status, string pin, bool visible);  
  15.         [OperationContract]  
  16.         void Delete(string Mob);  
  17.          
  18.     }  

Now to implement the methods. We are using a simple LINQ query for every method.


ImplementMethods

Then rebuild the solutions. And now we will able to consume all the methods in the page. Example of ShowAll().

  1. <html>  
  2. <head runat="server">  
  3.     <title></title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.          <h2>Display all Records</h2>  
  8.     <div>  
  9.     <br />  
  10.         <br />  
  11.         <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  12.     </div>  
  13.         <a href="Search.aspx" style="text-decoration:none; border: 2px solid #bfce43; font-size:18px; color:black;" >Search</a>  
  14.         <br />  
  15.         <a href="Insert.aspx"  style="text-decoration:none; border: 2px solid #bfce43; font-size:18px; color:black;">Insert</a>  
  16.     </form>  
  17. </body>  
  18. </html>  
  19.    
Code behind:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using BloodLINQ.ServiceReference1;  
  8.    
  9. namespace BloodLINQ  
  10. {  
  11.     public partial class Index : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();  
  16.             GridView1.DataSource = obj.ShowAll();  
  17.             GridView1.DataBind();  
  18.         }  
  19.     }  
  20. }  

The final output of the code. In this output we can see every HTTP verb, in this we can only see a GET method, only because we are fetching records, on the other methods we can see other operations too.

 
FinalOutPut

Summary

In this article we have learned WCF CRUD opeartions using LINQ to SQL Classes. We also learned 4 HTTP verbs (GET, PUT, POST and DELETE) and their uses. Thanks for reading.


Similar Articles