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 uses 4 methods to perform operations i.e. Insert, Delete, Update and Retrieve as mentioned below,
- Get: To retrieve a resource.
- Put: Creates or updates a resource with the provided details, ignoring completely, whatever is in there already, if anything.
- Delete: Delete the specific resource.
- 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.
- 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.
- 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.
- 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:
Step 2: Create your Service Contract and Data Member, as shown below:
- [ServiceContract]
- public interface IEmployee
- {
-
- [OperationContract]
- [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
- List<Employee> GetAllEmployee();
- }
- [DataContract]
- public class Employee
- {
-
- [DataMember]
- public string EmpID { get; set; }
-
- [DataMember]
- public string Name { get; set; }
- }
Step 3: Create your Supporting Classes, as given below:
- public static class EmployeDetails
- {
-
- public static List<Employee> GetAllEmploye()
- {
- List<Employee> employeeList = new List<Employee>()
- {
- new Employee() {EmpID = "720501" , Name = "Nishant"},
- new Employee() {EmpID = "720520" , Name = "Arjun"},
- new Employee() {EmpID = "887766" , Name = "Prashant"}
- };
-
- return employeeList;
- }
-
- }
Step 4: Implement an interface, as given below:
- public class EmployeeService : IEmployee
- {
- public List<Employee> GetAllEmployee()
- {
- List<Employee> employeeList=EmployeDetails.GetAllEmploye();
- return employeeList;
- }
}
Now, consume the Service and it will work fine.
The most important thing here is,
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.