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>
This is how the service’s response looks like in XML…
…and this is the JSON formatting:
That’s it. As you can see there is nothing Azure-specific, just plain WCF4.
Download a small sample solution.
Hope this helps someone.
Have you tried to deploy this solution in Windows Azure? When I have tried to deploy your aplication in Windows Azure it hang in "Busy" status. Only removing the section from the web.config has allowed to successfully deploy the solution in Azure environment.
-- Piotr
Posted by: Piotr | 07/07/2010 at 03:16 PM
Regarding to the last comment. It was a standardEndpoints section. Only removing it allow to successful deployment.
Posted by: Piotr | 07/07/2010 at 03:47 PM
I added the following section at the beginning of web.config file which fixed this issue:
Posted by: Rene | 07/14/2010 at 07:11 PM
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
Posted by: Rene | 07/14/2010 at 07:14 PM
I've tried to follow this tutorial with my own web service, and to download the attached package. Both have the same result: when I try to run it in VS2010 it hangs on "Launching the browser" and global.asax.cs is never run.
Does the package run as is in VS or does it need to be deployed to azure hosting to see it operate ?
Posted by: Andiih | 11/12/2010 at 01:33 PM
Hi Andiih,
yes, the sample from the download above works for me and several other people in the local dev fabric and in Azure.
Posted by: Christian Weyer | 11/13/2010 at 06:11 PM
Hi,
Thanks for this. It helped me save a lot of time and worked on Azure rightaway..
I did however have to add a multipleSiteBindingsEnabled ="true" attribute in the tag for this to work in IIS...strange because even in Azure it would have gotten installed on IIS itself but i kept getting a multibindings error untill i added this attribute. Any clue why this error did not arise in Azure deployed version???
Thanks
Cennest
www.cennest.wordpress.com
Posted by: Cennest | 12/10/2010 at 06:23 AM
Hi,
which Azure SDK version are you using?
Posted by: Christian Weyer | 12/10/2010 at 09:07 AM
I thought I will write up the very simple and straight-forward solution.
Posted by: Moncler Jackets | 01/05/2011 at 03:26 AM