Rest And RESTful WCF Service

Why I choose to write about this topic is I have personally found that there is a lot of misinformation and misconceptions around REST. Thus, the objective is to try and clear up all the doubts we have, by reading this article.

Before getting into what RESTful Service is, let’s try to dig into, what actually is REST?

REST

REST stands for “Representational state transfer” and works on HTTP protocols, but not always. REST is an architectural style, not a specification or a standard. The best thing about the REST is it’s not only restricted to XML formats. RESTful Service can send a plain text, JSON and also XML.You can say REST  is an alternative for SOAP and WSDL – based Web Services.
REST
REST uses 4 methods to perform operations i.e. Insert, Delete, Update and Retrieve as mentioned below,

  1. Get: To retrieve a resource.
  2. Put: Creates or updates a resource with the provided details, ignoring completely, whatever is in there already, if anything.
  3. Delete: Delete the specific resource.
  4. Post: Submits the data to be processed by the given resource.

Where to use Rest Service?

I have divided this question into a few points for detailed understanding.

  1. Minimize the coupling

    Coupling represents an interconnection b/w the units. The more coupling there is; the more dependent your program will be. Thus, we always prefer to have it less coupled. Hence, we can change the code according to the situation and it will not affect the existing code. REST is mostly suited for  when we have to write the code in a Client-Server environment. It may be a situation, when you may have to update your Server on a regular basis without needing to update the client.

  2. Heterogeneous Environment

    In another scenario, let's say your Server will be accessed by the heterogeneous clients. Server can be called from IPhone, Android, and Windows or by any other Service.

  3. HTTP Features

    When we use HTTP, we can use all the features of HTTP as well. The best feature is “Caching”. The benefit is to speed up and reduce the Server load.

Let us Start Creating our Restful WCF

Step 1:
Select WCF Service Application, as shown below:

WCF service

Step 2: Create your Service Contract and Data Member, as shown below:

code

  1. [ServiceContract]  
  2.     public interface IEmployee  
  3.     {  
  4.   
  5.         [OperationContract]  
  6.         [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]  
  7.         List<Employee> GetAllEmployee();  
  8.     }   
  9. [DataContract]  
  10.     public class Employee  
  11.     {  
  12.   
  13.         [DataMember]  
  14.         public string EmpID { getset; }  
  15.   
  16.         [DataMember]  
  17.         public string Name { getset; }  
  18.     }  
Step 3: Create your Supporting Classes, as given below:
  1. public static class EmployeDetails  
  2.     {  
  3.           
  4.         public static List<Employee> GetAllEmploye()  
  5.         {  
  6.             List<Employee> employeeList = new List<Employee>()  
  7.             {  
  8.                 new Employee() {EmpID = "720501" , Name = "Nishant"},  
  9.                 new Employee() {EmpID = "720520" , Name = "Arjun"},  
  10.                 new Employee() {EmpID = "887766" , Name = "Prashant"}  
  11.             };  
  12.   
  13.             return employeeList;  
  14.         }  
  15.   
  16.     }  
Step 4: Implement an interface, as given below: 
  1. public class EmployeeService : IEmployee  
  2.     {    
  3.         public List<Employee> GetAllEmployee()  
  4.         {  
  5.             List<Employee> employeeList=EmployeDetails.GetAllEmploye();  
  6.             return employeeList;  
  7.         }  
        }  
Now, consume the Service and it will work fine.

The most important thing here is,
code
I tried to invoke my service and it's working fine. Please see the screenshot, given below:

 

Point of Interest:

In this article we have seen what is rest? Where we can use restful service? And demo of small RESTful service.

 
I hope the article was useful and I was able to achieve my goal. Comments, suggestions and criticisms are all welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all