Designing And Creating WCF 4.0 REST Services

What is REST?

ReST is the 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: 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:

  1. Where the service is not complicated, (i.e. no requirements of security and state)

  2. When high level of scalability/performance is needed. (ReST allows for caching to increase the scalability)

  3. 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)

  4. ReST allows easy aggregation of services.

Designing ReST service

The ReST service itself has 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 includes a very important aspect of ReST; i.e. Resouce or Actor and 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 the following URI in our ReST service:

URI of ReST service

Here we've used the ?title={title} just to filter specific notes by title rather than showing all notes as there could be a number of notes in the record. This is all up to your design 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 interface in ReST service

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:

WCF ReST 4.0 template

The code below would be added to the Global.asax for request redirection:

    void Application_Start(object sender, EventArgs e)
    private void RegisterRoutes()
        RouteTable.Routes.Add(new ServiceRoute("HelloService", new WebServiceHostFactory(), typeof(Service1)));

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.

.Svc file in ReST service

Since in the web.config file the standardEndPoint tag has 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 do is simply introduce the code in the Service.cs file; to write a simple code to say hello:

    [WebGet(UriTemplate = "Message")]
    public string GetCollection()
        return "Hello there!! I'm a quick ReST message";

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

ReST service URL

See the HTML source of this page:

HTML source of ReST service

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.

Up Next
    Ebook Download
    View all
    View all