WCF REST on .Net 4.0
- by AngelEyes
A simple and straight forward article taken from:
http://christopherdeweese.com/blog2/post/drop-the-soap-wcf-rest-and-pretty-uris-in-net-4
Drop the Soap: WCF, REST, and Pretty URIs in .NET 4
Years ago I was working in libraries when the Web 2.0 revolution began. One of the things that caught my attention about early start-ups using the AJAX/REST/Web 2.0 model was how nice the URIs were for their applications. Those were my first impressions of REST; pretty URIs. Turns out there is a little more to it than that.
REST is an architectural style that focuses on resources and structured ways to access those resources via the web. REST evolved as an “anti-SOAP” movement, driven by developers who did not want to deal with all the complexity SOAP introduces (which is al lot when you don’t have frameworks hiding it all). One of the biggest benefits to REST is that browsers can talk to rest services directly because REST works using URIs, QueryStrings, Cookies, SSL, and all those HTTP verbs that we don’t have to think about anymore.
If you are familiar with ASP.NET MVC then you have been exposed to rest at some level. MVC is relies heavily on routing to generate consistent and clean URIs. REST for WCF gives you the same type of feel for your services. Let’s dive in.
WCF REST in .NET 3.5 SP1 and .NET 4
This post will cover WCF REST in .NET 4 which drew heavily from the REST Starter Kit and community feedback. There is basic REST support in .NET 3.5 SP1 and you can also grab the REST Starter Kit to enable some of the features you’ll find in .NET 4.
This post will cover REST in .NET 4 and Visual Studio 2010.
Getting Started
To get started we’ll create a basic WCF Rest Service Application using the new on-line templates option in VS 2010:
When you first install a template you are prompted with this dialog:
Dude Where’s my .Svc File?
The WCF REST template shows us the new way we can simply build services. Before we talk about what’s there, let’s look at what is not there:
The .Svc File
An Interface Contract
Dozens of lines of configuration that you have to change to make your service work
REST in .NET 4 is greatly simplified and leverages the Web Routing capabilities used in ASP.NET MVC and other parts of the web frameworks. With REST in .NET 4 you use a global.asax to set the route to your service using the new ServiceRoute class. From there, the WCF runtime handles dispatching service calls to the methods based on the Uri Templates.
global.asax
using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;
namespace Blog.WcfRest.TimeService
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private static void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("TimeService",
new WebServiceHostFactory(), typeof(TimeService)));
}
}
}
The web.config contains some new structures to support a configuration free deployment. Note that this is the default config generated with the template. I did not make any changes to web.config.
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
Building the Time Service
We’ll create a simple “TimeService” that will return the current time. Let’s start with the following code:
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace Blog.WcfRest.TimeService
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class TimeService
{
[WebGet(UriTemplate = "CurrentTime")]
public string CurrentTime()
{
return DateTime.Now.ToString();
}
}
}
The endpoint for this service will be http://[machinename]:[port]/TimeService. To get the current time http://[machinename]:[port]/TimeService/CurrentTime will do the trick.
The Results Are In
Remember That Route In global.asax?
Turns out it is pretty important. When you set the route name, that defines the resource name starting after the host portion of the Uri.
Help Pages in WCF 4
Another feature that came from the starter kit are the help pages. To access the help pages simply append Help to the end of the service’s base Uri.
Dropping the Soap
Having dabbled with REST in the past and after using Soap for the last few years, the WCF 4 REST support is certainly refreshing. I’m currently working on some REST implementations in .NET 3.5 and VS 2008 and am looking forward to working on REST in .NET 4 and VS 2010.