Exposing CRUD Operations as RESful WCF Service and Peforming all HTTP Operation at Client Side.

Objective:

This article will explain step by step, how to perform CRUD operation on a database using ADO.Net Data Service and then how to expose CRUD operations as REST enabled WCF service to the client. This will also give explanation, how to consume REST service at client side. This article will perform the entire HTTP verb on the service at the client Side.

A talk before we start

I have been surfing many blogs, sites to get some good articles on how to create RESTful WCF service for CRUD operation and then consumption at client side in one place. But I did not get any so I decided to write a consolidated article. Whoever will follow me in this article step by step, will able to create a RESTful service for all CRUD operation? This is my promise. Now let's start.

I must recommend, please read these 3 articles before starting with this article for better and complete understanding.

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/stepbystepadonetdataservice04272009085230AM/stepbystepadonetdataservice.aspx
http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/RESTEnabledService05122009034907AM/RESTEnabledService.aspx

And using WCF with ADO.Net Entity Model

Flow of the Article

1.gif

Step 1: Create Database

I have created a table named "WCF". This table got two columns Name and EmpId. Both are of data type nvarchar. EmpId is primary key.

2.gif

Table contains data as below,

3.gif

Just create table with said structure in database.

Step 2: Create Data Model


1. Open Visual studio, select File->New->Project->Web->ASP.Net Web Application. Give any meaningful name. I am giving name here DataService.

4.gif

2. Right click on DataService project in solution explorer and Add new Item. Go to Data tab and choose ADO.Net Entity Model. Either give any meaningful name or leave the default names. I am leaving the default names for this sample. Follow the pictorial depiction below. Dj is the name of the database. WCF is the name of the table. At the end clicking Finish, Data model will get created. You will have Model1.edmx in DataService project in solution explorer.

5.gif

6.gif

7.gif

8.gif

Model1.Edmx

9.gif

Step 3: Create ADO.Net Data Service

Here, we will create ADO.Net Data Service on the data model; we have created in Step 2.

1. Right click on DataService project in solution explorer and Add New Item. Go to Web tab and select ADO.Net Data Service option. Leave the entire default name or give as of your desire. For me, I am leaving the default names.

10.gif

2. Edit or modify the WebDataService1.svc.cs as below

WebDataService1.Svc.cs

11.gif

Please read this article written by me for complete description on ADO.Net Data Service

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/stepbystepadonetdataservice04272009085230AM/stepbystepadonetdataservice.aspx

3. Solution explorer will look like after all above steps as below.

12.gif

4. Debug the project and after successfully compilation, right click on WebDataService1.svc.cs and select View in Browser.

13.gif

Up to here, we have created Data Model and Ado.Net Data service on that Data model and tested that.

Step 4: Create REST enabled WCF Service

I must recommend, please go through below link to understand basic of; how to create REST service and URI construction in that.

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/RESTEnabledService05122009034907AM/RESTEnabledService.aspx

1. Right click on solution name in solution explorer and add a New Project by right clicking there. Select WCF Service Application. Give any name , I am giving name here RestTService.

14.gif

2. Delete all the default code generated. I am not changing here Contract name and Service implantation name, so it is IService1 and Service1.


3. Open Web.Config file and delete all the default EndPoint setting or in other words delete System.ServiceModel code.

4. Open markup of Servic1.svc, by right clicking and selecting View markup and modify the code as below.

<%@ ServiceHost Language="C#" Debug="true" Service="RestService.Service1" CodeBehind="Service1.svc.cs" Factory = "System.ServiceModel.Activation.WebServiceHostFactory"%>

If you are not able to understand what I am saying in above points, please go and read article at below link to have better understanding of the article.

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/RESTEnabledService05122009034907AM/RESTEnabledService.aspx

5. Now we need to create the DTO class. We will be creating this inside a class library. So to do so, add a new project in the solution of the type class library. Give it name either Businessclass or any name of your choice.

15.gif

6. Add a class WCFDTO in the class library project. This class will be acting as data transfer class between REST Service and client.

WCFDTO.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Runtime.Serialization; 

namespace BusinessClass
{
    [DataContract]
    public class WCFDTO
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string EmpId { get; set; }
    }
}

7. Compile the project.

8. Now returning back to RestService. Right clcik to this project and add

  • Refernece of System.ServiceModel.Web

  • Project reference of BusinessClass. This class we created just now in 6th step.

  • Add Service Reference of ADO.Net Data Service. This service we created in Step 3.

9. We need to create a converetr class , this class will convert WCFDTO class to WCF class. Note here that WCF class is generated by Data model inm Step2. So Rest service will able to use this class to pefrom all operation because we have added service reference of the ADO.Net Data service.
So for this right click and add a class in RestService project. Give name of this class as Converter. This is a static class. This class is having two static methods , one to convert WCF class instance to WCFDTO class instance and other class to convert WCFDTO class instance to WCF class instance.

Converter.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
RestService.ServiceReference1;
using
BusinessClass; 

namespace RestService
{
    public static class Converter
    {
        public static WCFDTO ConvertWCFtoWCFDTO(WCF obj)
        {
            WCFDTO objdto = new WCFDTO();
            objdto.EmpId = obj.EmpId;
            objdto.Name = obj.Name;
            return objdto;
        } 

        public static WCF ConvertWCFDTOtoWCF(WCFDTO obj)
        {
            WCF objdto = new WCF();
            objdto.EmpId = obj.EmpId;
            objdto.Name = obj.Name;
            return objdto;
        }
    }
}

10. Now we will create the contract. Contract got 5 operations.

IService1.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
using
System.ServiceModel.Web;
using
BusinessClass; 

namespace RestService
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate="/WCF/{empid}")]
        WCFDTO Find(string empid);

        [OperationContract]
        [WebGet(UriTemplate = "/WCF/")]
        List<WCFDTO> All(); 

        [OperationContract]
        [WebInvoke(UriTemplate="/WCF",Method="POST")]
        void InsertData(WCFDTO obj); 

        [OperationContract]
        [WebInvoke(UriTemplate = "/WCF", Method = "PUT")]
        void Updatedata(WCFDTO obj); 

        [OperationContract]
        [WebInvoke(UriTemplate = "/WCF/{id}", Method = "DELETE")]
        void DeleteData(String id);
    }
}

Note: For Basic detail of REST Service and how to construct URI, please surf to below link. Here I am not explaining the details.

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/RESTEnabledService05122009034907AM/RESTEnabledService.aspx

11. Service implantation

Service1.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
using
RestService.ServiceReference1;
using
BusinessClass; 

namespace RestService
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        kpmgdemoEntities _ent ; 

        public WCFDTO Find(string Empid)
        {
            _ent = new kpmgdemoEntities(new Uri("http://localhost:1098/WebDataService1.svc"));
            WCF res = (from r in _ent.WCF where r.EmpId == Empid select r).First();
            return Converter.ConvertWCFtoWCFDTO(res);
        } 

        public void InsertData(WCFDTO obj)
        {
            _ent = new kpmgdemoEntities(new Uri("http://localhost:1098/WebDataService1.svc"));
            WCF insertingobject = Converter.ConvertWCFDTOtoWCF(obj);
            _ent.AddObject("WCF", insertingobject);
            _ent.SaveChanges();
        } 

        public void Updatedata(WCFDTO obj)
        {
            _ent = new kpmgdemoEntities(new Uri("http://localhost:1098/WebDataService1.svc"));
            WCF res = (from r in _ent.WCF where r.EmpId == obj.EmpId select r).First();
            res.Name = obj.Name;
            _ent.UpdateObject(res);
            _ent.SaveChanges();
        } 

        public void DeleteData(String id)
        {
            _ent = new kpmgdemoEntities(new Uri("http://localhost:1098/WebDataService1.svc"));
            WCF res = (from r in _ent.WCF where r.EmpId == id select r).First();
            //WCF insertingobject = Converter.ConvertWCFDTOtoWCF(obj);
            _ent.DeleteObject(res);
            _ent.SaveChanges();
        }

        public List<WCFDTO> All()
        {
            _ent = new kpmgdemoEntities(new Uri("http://localhost:1098/WebDataService1.svc"));
            var res = from r in _ent.WCF select r;
            List<WCFDTO> abc = res.ToList().ConvertAll(new Converter<WCF, WCFDTO>(Converter.ConvertWCFtoWCFDTO));
            return abc;
        }
    }
}


Note: Please follow the below link for details on CRUD operation in ADO.Net Data Service.

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/stepbystepadonetdataservice04272009085230AM/stepbystepadonetdataservice.aspx

12. Compile and run the Service.
 

16.gif

Up to this Step, we have created our REST WCF Service and tested that in the browser.

Step 5: Create client to consume the REST WCF Service

  1. Create a client either entirely different project or by right clicking at the solution and add new project. I am here adding in the same solution new Console project as client. In this Console Project, I am going to consume the REST WCF service.

  2. Add reference of Microsoft.Http.dll in the console project.

  3. Add project reference of BusinessClass . Such that client can understand WCFDTO class.

  4. I have created a ServiceUriHelper class. This class will construct URI on which client can perform operation.

    ServiceUriHelper.cs

    public static class ServiceUriHelper
        {
            static String ServiceUri;
            static ServiceUriHelper()
            {
                ServiceUri = "http://localhost:1121/Service1.svc/".Trim().TrimEnd('\\');
            } 

            public static String GetServiceUri(EntityAction action)
            {
                switch (action)
                {
                    case EntityAction.Insert :
                        return ServiceUri + @"WCF";
                    case EntityAction.Update :
                        return ServiceUri + @"WCF";
                    case EntityAction.Delete:
                        return ServiceUri + @"WCF/";
                    case EntityAction.Load:
                        return ServiceUri + @"WCF/";
                    case EntityAction .Select :
                        return ServiceUri + @"WCF/";
                    default:
                        return String.Empty;
                }
            }
        }

        public enum EntityAction
        {
            Insert =0,
            Update = 1,
            Delete = 2,
            Select = 3,
            Load = 4

        }



    Explanation:

    1. In constructor we are initializing a variable with the base URI of the service.

      ServiceUri = "http://localhost:1121/Service1.svc/".Trim().TrimEnd('\\');

      In this case service is running on localhost:1121. In your case, it would be different. Just chek out in browser , while running your service and copy paste from the address bar of the browser to the here.

    2. Since for all operation different URI are there , so on basis of operation we are amening the service uri here in the select statement.
       

  5. Insert Operation

    We are calling POST method of HttpClient class. And passing serialized data contract.

    public static void InsertData(WCFDTO obj)
    {
                using (HttpResponseMessage respone = new HttpClient().Post(ServiceUriHelper.GetServiceUri(EntityAction.Insert), HttpContentExtensions.CreateDataContract(obj)))
                {
                };

    }
     

  6. Update Operation

    We are calling PUT method of HttpClient class. And passing serialized data contract.

    public static void UpdateData(WCFDTO obj)
    {
                using (HttpResponseMessage response = new HttpClient().Put(ServiceUriHelper.GetServiceUri(EntityAction.Update), HttpContentExtensions.CreateDataContract(obj)))
                {
                };
    }
     

  7. Delete Operation

    We are calling DELETE method of HttpClient class. And passing serialized data on which delete is to be performed.

    public static void DeleteData(WCFDTO obj)
    {
                using (HttpResponseMessage response = new HttpClient().Delete(ServiceUriHelper.GetServiceUri(EntityAction.Delete)+obj.EmpId))
                {
                };

    }
     

  8. Select Operation

    We are passing the EmpId to select the particular record. At HttpResponse message, we are getting the XML data and we desearlizing that using the
    response.content.ReadDataContract

    public static WCFDTO SelectRecord(String id)
    {
                using(HttpResponseMessage response = new HttpClient().Get(ServiceUriHelper.GetServiceUri(EntityAction.Select)+id))
                {
                    return response.Content.ReadAsDataContract<WCFDTO>();
                };
    }

     

  9. Load Operation

    This method will fetch all the records from the table. Here we are desearlizing List of data contract.

    public static List<WCFDTO> GetAllRecord()
    {
                using (HttpResponseMessage response = new HttpClient().Get(ServiceUriHelper.GetServiceUri(EntityAction.Load)))
                {
                    return response.Content.ReadAsDataContract<List<WCFDTO>>();
                };

    }

     
So, the entire client will look like

Program.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.ServiceModel.Web;
using
Microsoft.Http;
using
Microsoft.ServiceModel.Web.SpecializedServices;
using
BusinessClass;
using
System.Runtime.Serialization; 

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            List<WCFDTO> res = GetAllRecord();
            foreach (WCFDTO r in res)
            {
                Console.WriteLine(r.EmpId + "\t" + r.Name);
            }
            Console.Read(); 

            WCFDTO obj = new WCFDTO();
            obj.EmpId = "E11";
            obj.Name = "Rekha Singh";
            InsertData(obj);
            Console.WriteLine("New Record Has been Inserted ");
            Console.Read();
            List<WCFDTO> res1 = GetAllRecord();
            foreach (WCFDTO r in res1)
            {
                Console.WriteLine(r.EmpId + "\t" + r.Name);
            }
            Console.Read();
            obj.Name = "Rekha S";
            UpdateData(obj);
            Console.WriteLine("Record has been Updated ");
            Console.Read();
            List<WCFDTO> res3 = GetAllRecord();
            foreach (WCFDTO r in res3)
            {
                Console.WriteLine(r.EmpId + "\t" + r.Name);
            }
            Console.Read();
            DeleteData(obj);
            Console.WriteLine("Record Has been deletd");
            Console.Read();
            List<WCFDTO> res4 = GetAllRecord();
            foreach (WCFDTO r in res4)
            {
                Console.WriteLine(r.EmpId + "\t" + r.Name);
            }
            Console.Read();
        }

        public static void InsertData(WCFDTO obj)
        {
            using (HttpResponseMessage respone = new HttpClient().Post(ServiceUriHelper.GetServiceUri(EntityAction.Insert), HttpContentExtensions.CreateDataContract(obj)))
            {
            };
        } 

        public static void UpdateData(WCFDTO obj)
        {
            using (HttpResponseMessage response = new HttpClient().Put(ServiceUriHelper.GetServiceUri(EntityAction.Update), HttpContentExtensions.CreateDataContract(obj)))
            {
            };
        } 

        public static void DeleteData(WCFDTO obj)
        {
            using (HttpResponseMessage response = new HttpClient().Delete(ServiceUriHelper.GetServiceUri(EntityAction.Delete)+obj.EmpId))
            {
            };
        } 

        public static List<WCFDTO> GetAllRecord()
        {
            using (HttpResponseMessage response = new HttpClient().Get(ServiceUriHelper.GetServiceUri(EntityAction.Load)))
            {
                return response.Content.ReadAsDataContract<List<WCFDTO>>();
            };
        } 

        public static WCFDTO SelectRecord(String id)
        {
            using(HttpResponseMessage response = new HttpClient().Get(ServiceUriHelper.GetServiceUri(EntityAction.Select)+id))
            {
                return response.Content.ReadAsDataContract<WCFDTO>();
            };
        }

    }

     public static class ServiceUriHelper
    {
        static String ServiceUri;
        static ServiceUriHelper()
        {
            ServiceUri = "http://localhost:1121/Service1.svc/".Trim().TrimEnd('\\');
        } 

        public static String GetServiceUri(EntityAction action)
        {
            switch (action)
            {
                case EntityAction.Insert :
                    return ServiceUri + @"WCF";
                case EntityAction.Update :
                    return ServiceUri + @"WCF";
                case EntityAction.Delete:
                    return ServiceUri + @"WCF/";
                case EntityAction.Load:
                    return ServiceUri + @"WCF/";
                case EntityAction .Select :
                    return ServiceUri + @"WCF/";
                default:
                    return String.Empty;
            }
        }
    }

    public enum EntityAction
    {
        Insert =0,
        Update = 1,
        Delete = 2,
        Select = 3,
        Load = 4
    }
}


Conclusion:

This article explained about, building a REST ful WCF operation for CRUD operation on a table using ADO.Net Data Service. This also explained how to consume a REST service in a client.

How to use Zip File?

Just download the ZIP file and change the Data Model, which we discussed in Step 2. Even if you want to start from beginning, content of ZIP file will help you in understanding better,

Thank you for Reading. Happy Coding

Up Next
    Ebook Download
    View all
    Learn
    View all