Beautiful REST services URLs in Windows Azure with WCF4’s routing integration feature

Update: It turns out that the necessary WCF4 config sections for standardEndpoints are not present in Windows Azure, although we are using a .NET 4-based Azure role :( Thanks for my readers Piotr and Rene for the hint! The config below has been updated.

We all know how certain people hate the .svc in URLs when they are designing and building REST services with WCF. Just because I had a question from a customer today how to have ‘beautiful’ URLs for a WCF service being hosted in Windows Azure I thought I will write up the very simple and straight-forward solution.

The only pre-requisite you need is .NET 4.0 (and thus WCF4).

WCF4 has a nice feature I like really a lot. It is the integration into the System.Web.Routing engine when it comes to hosting your services. You can simply add a route to your service implementations in the global.asax file – as seen in the code below. No need for .svc files, yiha!

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.ServiceModel.Activation" %>
<%@ Import Namespace="Thinktecture.TecTeacher.MediaServices" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e) 
    {
        RouteTable.Routes.Add(
               new ServiceRoute(
                   "media", new WebServiceHostFactory(),
                   typeof(MediaService)));
    }          
</script>

Together with the also new automatic help page feature and the automatic response format selection in WCF4 you get a nice REST service hosted in IIS. And obviously this can also be used in a Windows Azure Web Role which leverages IIS’s hostable web core (HWC).

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.serviceModel"
                  type="System.ServiceModel.Configuration.ServiceModelSectionGroup, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="standardEndpoints"
               type="System.ServiceModel.Configuration.StandardEndpointsSection, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="false"
                 targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true"
                          automaticFormatSelectionEnabled="true">
          <security mode="None"/>
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

WCF REST service 

WCF REST service help 

WCF REST service call help

This is how the service’s response looks like in XML…

WCF REST service call result in XML

…and this is the JSON formatting:

WCF REST service call result in JSON

That’s it. As you can see there is nothing Azure-specific, just plain WCF4.
Download a small sample solution.

Hope this helps someone.