WCF, REST and URL Rewriting with Windows Azure!

So there is nothing I find nicer than a well formatted, REST based URL.

Take for example:

https://myhost/customer/2/orders

As a way to retrieve all orders for customer number 2.

There is also nothing I like more than WCF! The flexibility to declaratively control the behavior of my web service is perfect for situations where I need to respond quickly to heterogeneous client calling requirements (REST, SOAP, etc).

And finally, I love Windows Azure, nothing more really to say there.

So how do you build a RESTful WCF Service in Windows Azure?

Start with a new Cloud project:

image

Then add a new WCF Service Web Role:

image

Now, this is enough to get a WCF service going in Windows Azure, so just hit F5 and you’ll get this:

image

If you don’t get this, then something is not right. First thing to check is that you’ve switched on WCF activation:

image

If this doesn’t help matters, then you’re out of luck my furry friend. Time to hit the power button and try again.

Next we want to change our service’s behavior to be more REST’y, so we start by changing the way the page is hosted. To do this, we set the Factory property of our ServiceHost by editing the markup of our service to use the WebServiceHostFactory:

image

Now, the first time you run this, you’re going to hit a 400:

image

Simply go into your web.config and remove the system.serviceModel section, as the WebServiceHostFactory will take care of your services behavior. (Some more helpful info on RESTful services with WCF here)

This should get your service going, but you’ll notice that the WebServiceHostFactory will not have any endpoints enabled.

image

All good, onto our next part. To make sure we can invoke our service through a HTTP verb, and the response can be returned in basic XML, we need to add some attributes to our OperationContract:

image

So now, you can simply invoke your service like such:

image

Getting closer.

But I don’t like having the .svc extension in the URL. One of the great things about REST, well, web services in total, is you don’t have to expose the implementation details to the caller. So let’s get rid of the .svc part. To do this, we use a fantastic feature available in Windows Azure called the URL Rewrite module. So I write a crude and dirty little rule in my web.config that maps any calls to myservice to Service1.svc.

image

Now we’re almost there:

image

Now, to make URL Rewriting work on your local machine, remember to install the appropriate version of the URL Rewrite Module on your machine, per these instructions.

Now, not happy to leave well enough alone, I want to pass in my value as part of the URI, rather than part of the query string, so I’m going to set the UriTemplate property of the WebGet attribute to reflect my formatting requirements:

image

You’ll also notice I had to change the input type of my method to a string, as the UriTemplate requires that parameters, in this example, {value}, are string types.

Now, everything is as it should be, and I can call my service straight from a HTTP verb, in this case, GET, using a nicely formatted URI:

image

This all runs nicely in Windows Azure, and more importantly, is callable from any client capable of constructing a HTTP payload, so the interop is awesome!

Here is the basic Windows Azure project in VS2010 format.

Enjoy!

Technorati Tags: Windows Azure,WCF,REST,URL Rewrite Module