IT Story/C# & WPF

How to create REST service

Hoyami7 2019. 12. 12. 21:50
반응형

This sample explains about the creating the RESTful service to create and updating the resource information available at the sever side. This Restful service will be consumed using client console application.

Step 1: For our example we are suing “EmployeeData” class as Data Access Layer for storing and reading the employee information.

 

namespace MyFirstRESTfulService
{
    [DataContract]
    public class Employee
    {
        [DataMember]
        public int EmpId { get; set; }
        [DataMember]
        public string Fname { get; set; }
        [DataMember]
        public string Lname { get; set; }
        [DataMember ]
        public DateTime JoinDate { get; set; }
        [DataMember]
        public int Age { get; set; }
        [DataMember]
        public int Salary { get; set; }
        [DataMember]
        public string Designation { get; set; }
    }


    public partial class EmployeeData
    {
        private static readonly EmployeeData _instance = new EmployeeData();

        private EmployeeData() { }

        public static EmployeeData Instance
        {
            get
            {
                return _instance;
            }
        }


        private List< Employee> empList = new List < Employee>()
        {
            new Employee() { EmpId  = 1, Fname = "Sam", Lname = "kumar", JoinDate=new DateTime(2010,7, 21), Age=30,Salary=10000,Designation="Software Engineer"},
            new Employee() { EmpId = 2, Fname = "Ram", Lname = "kumar", JoinDate=new DateTime(2009,6,8), Age=35,Salary=10000,Designation="Senior Software Engineer"},    
            new Employee() { EmpId = 3, Fname = "Sasi", Lname = "M", JoinDate=new DateTime(2008,3,5), Age=39,Salary=10000,Designation="Projet Manager"},  
            new Employee() { EmpId = 4, Fname = "Praveen", Lname = "KR", JoinDate=new DateTime(2010, 5,1), Age=56,Salary=10000,Designation="Projet Manager"},
            new Employee() { EmpId = 5, Fname = "Sathish", Lname = "V", JoinDate = new DateTime(2006,12,15), Age=72,Salary=10000,Designation="Senior Software Engineer"},  
            new Employee() { EmpId = 6, Fname = "Rosh", Lname = "A", JoinDate=new DateTime(2009,2,2), Age=25,Salary=10000,Designation="Software Engineer"}
        };

        public List< Employee> EmployeeList
        {
            get
            {
                return empList;
            }
        }


        public void Update(Employee updEmployee)
        {
            Employee existing = empList.Find(p => p.EmpId == updEmployee.EmpId);

            if (existing == null)
                throw new KeyNotFoundException("Specified Employee cannot be found");

            existing.Fname = updEmployee.Fname;
            existing.Lname = updEmployee.Lname;
            existing.Age = updEmployee.Age;
        }

        public void Delete(int empid)
        {
            Employee existing = empList.Find(p => p.EmpId == empid);
            empList.Remove(existing);
        }
        public void Add(Employee newEmployee)
        {
            empList.Add(new Employee
            {
                EmpId = newEmployee.EmpId,
                Fname = newEmployee.Fname,
                Lname = newEmployee.Lname,
                Age = newEmployee.Age,
                JoinDate = DateTime.Now,
                Designation = newEmployee.Designation,
                Salary = newEmployee.Salary
            });
        }
    }
}

Step2: This service can be hosted as normal WCF service by creating the ServiceHost object and adding endpoint with different binding. This is already explained in “ConsoledHosted WCF Service”.

As it is mention in introduction section of RESTful service, all the resource located in server side can be accessed using url. Method exposed at the server side can be call using url, to do that we need to decorate the service method with “WebGet” or “WebInvoke” attribute as mention below

public interface IEmployeeService
    {
        [WebGet(UriTemplate = "Employee")]
        [OperationContract]
        List< Employee > GetAllEmployeeDetails();

        [WebGet(UriTemplate = "Employee?id={id}")]
        [OperationContract]
        Employee GetEmployee(int Id);

        [WebInvoke(Method = "POST", UriTemplate = "EmployeePOST")]
        [OperationContract]
        void AddEmployee(Employee newEmp);

        [WebInvoke(Method = "PUT", UriTemplate = "EmployeePUT")]
        [OperationContract]
        void UpdateEmployee(Employee newEmp);

        [WebInvoke(Method = "DELETE", UriTemplate = "Employee/{empId}")]
        [OperationContract]
        void DeleteEmployee(string empId);
    }

Step4: In the above interface declaration, you can find that we have added UriTemplate”, it is nothing but a relative path for accessing the service method using url. These methods can be called from client application or browser by typing url as “WCf Service url” + “Relative Path” E.g: http://localhost:8090/MyService/EmployeeService/Employee

Step 5: “Method” is another option we can add to the WebInvoke attribute to specify the mode of transfer like “PUT”, “POST”, or “DELETE”

Step 6: Now we have completed with service implementation project. Let’s start with Hosting the RESTful service. For this example we are using console application for hosting service. WCF framework has provided new class to host the RESTful service i.e WebServiceHost. By hosting the restful service with WebServiceHost host class will automatically set the binding and other configuration setting. In the below code you can see that I have only mention url for the hosting.

반응형