stands for. This is a protocol for exchanging data over a distributed environment. The main idea behind REST is that we should treat our distributed services as a resource and we should be able to use simple HTTP protocols to perform various operations on that resource.
When we talk about the Database as a resource we usually talk in terms of operations. i.e. Create, Retrieve, Update and Delete. Now the philosophy of REST is that for a remote resource all these operations should be possible and they should be possible using simple HTTP protocols.
Now the basic CRUD operations are mapped to the HTTP protocols in the following manner:
1) : this map to the part of the CRUD operation. This will be used to retrieve the required data (representation of data) from the remote resource.
2) : this map to the part of the CRUD operation. This protocol will update the current representation of the data on the remote server.
3) : This maps to the part of the CRUD operation. This will create a new entry for the current data that is being sent to the server.
4) : This maps to the part of the CRUD operation. This will delete the specified data from the remote server.
So if we take a hypothetical example of a remote resource that contains a database of list of books. The list of books can be retrieved using a URL like:
To retrieve any specific book, let’s say we have some ID that we can used to retrieve the book; the possible URL might look like:
Since these are requests, data can only be retrieved from the server. To perform other operations, if we use the similar URI structure with , or operation, we should be able to create, update and delete the resource form the server. We will see how this can be done in implementation part.
Using the code
Now we can create a simple WCF service that will implement all the basic CRUD operations on some database. But to make this WCF service REST compatible we need to make some changes in the configuration, service behaviors and contracts. Let us see what WCF service we will be creating and then we will see how we can make useful over the REST protocol.
Creating REST enabled ServiceContract
We will create Books table and will try to perform CRUD operations on this table.
To perform the Database operations within the service let’s use Entity framework. This can very well be done by using ADO.NET calls or some other ORM but I chose entity framework. The generated Entity will look like following.
Now the service contract will contain functions for CRUD operations. Let us create the for this service:
Right now this is a very simple service contract, to indicate that individual operations can be called using REST protocol, we need to decorate the operations with additional attributes. The operations that are to be called on HTTP protocol, we need to decorate them with the attribute. The operations that will be called by protocols, like will be decorated with attribute.
Now before adding these attributes to these operations let us first understand the concept of .
is a property of WebGet and attribute which will help us to map the parameter names coming from the HTTP protocol with the parameter names of . For example, if someone uses the following URI:
We need to map this first parameter with the id variable of the function. this can be done using the . Also, we can change the function name specifically for the URI and the name of URI function name will be mapped to the actual function name i.e. if we need to call the same URL as:
Then we can do that by specifying the for the operation as:
Following the same lines, let us define the for other methods too.
Implementing the Service
Now the service implementation part will use the entity framework generated context and entities to perform all the respective operations.
Restful WCF service Configuration
Now from the ServiceContract perspective the service is ready to serve the REST request but to access this service over rest we need to do some changes in the service behavior and binding too.
To make the service available over REST protocol the binding that needs to be used is the webHttpBinding. Also, we need to set the endpoint's behavior configuration and define the webHttp parameter in the endpointBehavior. So our resulting configuration will look something like:
Test the service
Now to test the service we will simply run the service and use the URLs to retrieve the data. Let see this for our GET operations in action.
And so we have seen that we received the response in the browser itself in form of XML. We can use this service without even consuming it by adding a service reference by using the URLs and HTTP protocols.
We can also change the Response and Request format to use instead of . To do this we need to specify properties of the attribute.
1) : By default its value is . To change it to JSON format, it needs to be set to .
2) : By default its value is . To change it to JSON format, it needs to be set to
Let us create one more operation in our service contract called as and will apply the as Json for this method.
The response will now appear in the JSON format.