I used to post answers in a .Net forum and many times I saw a question there What are ReST services, How to create a ReST service and that made me curious about it and I came to write this post. so very first of all I would like to Introduce the word ReST.
What is REST?
ReST is abbreviation of Representation State Transfer So I chose to write it like ReST rather than REST. It is an architectural style that focuses on resources and structured ways to access those resources via the web. It deals with sending data as XML message over the HTTP. Like SOAP it doesn’t hold Header, Body or Envelop to wrap the payload. Its more of URI based for e.g. such a service would be available to me at the following URI:http://host.com/ServiceDomainName/ServiceName. This is just an example.
Does it support the authentications and other security?
ReST services doesn’t keep any information like SOAP headers does. But the authentication can be implemented using the some customization in URI by embedding some Hashed security token in it. Common places/situations where ReST can be preferred to use:
- Where the service is not complicated, (i.e. no requirements of security and state)
- When high level of scalability/performance is needed. (ReST allows for caching to increase the scalability)
- Bandwidth is of importance and needs to be limited. (In SOAP we are passing a lot of extra data all the time, ReST allows for URI based identification of resources)
- ReST allows easy aggregation of services.
Designing ReST service
The ReST service itself have its development cycle. you don’t want to skip the designing phase if you’ve decided to use the ReST service in your application. This phase include a very important aspect of ReST i.e. Resource or Actor, URI templates. The first step in designing a ReSTful service is to identify the resources the service will expose. for e.g.
- User
- Notes
These two resources can be further manipulated for multiple operations that would help us designing the URI template later on. So the possible combination of these resources with operations could be:
- Individual User
- Individual User Profile
- Individual Note
- Collection of all public Notes
- User’s Private Notes
- User’s All Public Notes
Now the next phase would be the deciding and defining the design of URI template. So considering the above resources we’ll have following URI in our ReST service:
Operations | Descriptive URI format |
Individual User | /users/{username} |
Individual User Profile | /users/{username}/profile |
Individual Note | users/{username}/notes/{noteid} |
Collection of all public Notes | ?title={title} |
User’s Private Notes | /users/{username}/notes?title={title} |
User’s all Public Notes | {username}?title={title} |
Here we’ve used the ?title={title} just to filter specific notes by title rather than showing all notes as there could be number of notes in the record. This is all up to your designing strategy this was just to show you how to use the approach. I hope this would ensure you covering everything with their scope.
ReST can perform following operations using the HTTP interface:
HTTP Verb | Operation | Response Code |
GET | Retrieve {user}/{note} | 200 (“OK”), 404(“Not Found”) |
POST | Create/Update {user}/{note} | 200 (“OK”), 404(“Not Found”) |
PUT | Create/Update {user}/{note} | 200(“OK”), 401 (“Unauthorized”) |
DELETE | Delete {user}/{note} | 200(“OK”) means Successful, 401 (“Unauthorized”) |
This is all information that would help you understanding the ReST first. For further detailed reading here is the MSDN white paper.
ReST in WCF 4.0
Now lets get started with some hands on exercise on ReST. To start with the ReST services development I recommend you to download and Install the WCF ReST template from Microsoft WCF team that’ll save your lots of time Download from here.
There you might be surprising that few files would be missing that you used to have in your simple WCF web service:
- The .Svc File
- An Interface Contract
- Dozens of lines of configuration that you have to change to make your service work
other than the changes in the Web.configurations file there’ll be a Global.asax file that is having a code to handle the Request coming using the WebRoutine class of Asp.Net MVC.
If you have previously worked with WCF ReST services than you can clearly say the difference between the setting you’d used in web.config file. The new config file will look something like this in the WCF ReST 4.0 template:
The below code would be added Global.asax for request redirection
so the url requesting the resource at http://localhost/wcfrest2/helloservice/ will send the request to the Service actually so you don’t need the .Svc file.
Since in the web.config file the standardEndPoint tag have helpEnabled=”true” so you can just use the help resource for your service to see all the available resources at possible URIs. Now What you have to is simply introduce the code in Service.cs file write a simple code to say hello
You can host the ReST service in IIS with easy steps. Just go to Project Properties –> Click on Web tab –> Check Use IIS –> Create Virtual directory. Make sure you are running the Visual Studio 2010 under Administrator privilege.
Now go to your service URL http://localhost/wcfrest2/helloservice/Message
See the HTML source of this page
This your ReST format of response came from the WCF service. So after reading this post you will be able to start designing and creating WCF ReST with VS2010.
Hope you enjoyed the ReST show. Please leave comment of suggestions/questions.