Hello Everyone, I am back with a new blog on REST services and how we can make a restful service with prior knowledge of WCF service in .Net.
Lets try to understand what is REST service and how it is better than WCF service
Overview:
REST stands for Representational State Transfer is an architectural style of building services that can support a large number of network based applications. It uses HTTP verbs to perform CRUD operations. As it follows HTTP protocol it is stateless in nature.
Following are the HTTP verbs used in REST architecture:
1. GET : It retrieves the collection of resources. It is similar to READ operation
2. POST : It create a collection of resources. It is similar to CREATE operation
3. PUT : It replaces the entire collection of resources with new one
4. PATCH: It replaces the specified resources in the collection. It is similar to UPDATE operation
5.DELETE: It removes the resource from collection. It is similar to DELETE operation
So let's see how we can use WCF knowledge to build a REST service
Step 1:
Go to Visual Studio-->File-->New Project-->WCF Service Application
Once you selected the template give it a name it will generate a Project for you. In this case i have given a name of Demo Service
Step 2:
Once the project is created it will generate 2 file as
1. IService.cs
2. Service.svc.cs
Let's see What is IService.cs file
IService.cs : This is the interface file that has all the operation contracts that a service is going to exposed to its clients. It means that it exposes all the methods that the service offers to its end users.
As we can see, our operation contract has some extra attributes on it. Let's try to understand what this attributes are:
1. WebInvoke : This attribute specifies how your service will expect service calling from clients to perform operations on resources, It has several parameter and we will discuss them one by one
Lets try to understand what is REST service and how it is better than WCF service
Overview:
REST stands for Representational State Transfer is an architectural style of building services that can support a large number of network based applications. It uses HTTP verbs to perform CRUD operations. As it follows HTTP protocol it is stateless in nature.
Following are the HTTP verbs used in REST architecture:
1. GET : It retrieves the collection of resources. It is similar to READ operation
2. POST : It create a collection of resources. It is similar to CREATE operation
3. PUT : It replaces the entire collection of resources with new one
4. PATCH: It replaces the specified resources in the collection. It is similar to UPDATE operation
5.DELETE: It removes the resource from collection. It is similar to DELETE operation
So let's see how we can use WCF knowledge to build a REST service
Step 1:
Go to Visual Studio-->File-->New Project-->WCF Service Application
Once you selected the template give it a name it will generate a Project for you. In this case i have given a name of Demo Service
Step 2:
Once the project is created it will generate 2 file as
1. IService.cs
2. Service.svc.cs
Let's see What is IService.cs file
IService.cs : This is the interface file that has all the operation contracts that a service is going to exposed to its clients. It means that it exposes all the methods that the service offers to its end users.
As we can see, our operation contract has some extra attributes on it. Let's try to understand what this attributes are:
1. WebInvoke : This attribute specifies how your service will expect service calling from clients to perform operations on resources, It has several parameter and we will discuss them one by one
- Method: What type of HTTP verb it is expecting i.e. GET,POST,PUT,PATCH or DELETE
- RequestFormat: The format of the request made to the service. It can be either JSON or XML
- ResponseFormat: The format of the response from the service to end user. It can be either JSON or XML
- UriTemplate: The Uniform Resource identifier i.e. the identifier of the operation contracts. If URI is initialized then the calling URL must contain the URI initialized string
Note: This attribute is present in System.ServiceModel.web namespace and it is must to be used in REST service
2. WebGet: This attribute is only used for retrieving any resource
Step 3:
Service.svc.cs file:
This file contain the actual implementation of our operations contracts. This file implements the IService.cs file
In this demo Service we are returning a employee resource whenever a GET call is received.
Step 4:
Web.config file:
Here the binding that is used is WebHttp binding . Almost everything is similar to WCF service configuration except Binding.
Once completed just right click the Service.svc.cs file and click browse option. Below are the screenshot after browsing
WSDL document
Output:
JSON Format:
XML Format:
Hoping everyone must have understood how we can use WCF service template to make a REST service.
Thanks everyone and Happy coding!!!!
No comments:
Post a Comment