Last week at MIX11, Preview 4 of WCF Web API was released. If you missed it, you can watch the video here. One way to get Preview 4 and start playing with it is to download it here. However, an even easier way to to just grab the bits from NuGet:

When you download the latest Web API bits, there are a couple of sample applications that show off the latest features of the Web API (these are in the CodePlex download, not the NuGet packages). The first one is a Contact Manager “Simple” project which is intended to show just how simple it is to consume the Web API framework. There is also another sample which shows the advanced features such as IoC support, custom media type formatters, channels, and more.

I like to create a lot of “Hello World” applications when I’m learning a new technology for the first time or even when I just want to do some quick tests for a technology I already know pretty well. The existing Web API NuGet packages certainly help us add the references quickly. However, there is an even quicker way to get up and running with the new Web API – sample NuGet packages. Since NuGet has such rich functionality beyond adding references such as adding start code and configuration, there are many packages that exist to get you up and running fast. David Ebbo highlighted this last month in his post: Take NuGet to the next level with sample packages.

Taking all this into account, I created a “WebApi.CrudHttpSample” NuGet package. This allows you to fire up a brand new instance of Visual Studio, File – New Project, add the package and you are up and running. The Web API assemblies are added via NuGet dependencies and starter code is added as well. You can just select an “ASP.NET Empty Web Application” without having to worry about selecting some special WCF project template. This will have nothing in it but an empty web.config file. Now you can add the package:

PM> Install-Package WebApi.CrudHttpSample

This first thing you’ll see is a “dummy” model class that looks like this:

Use Visual Studio refactoring to rename this class. It’s important to use the VS.NET refactoring because the type is referenced elsewhere in the starter code so this will rename it appropriately everywhere. I’ll rename it to “Contact” and add a couple of properties to it:

1: publicclass Contact

2: {

3: publicint Id { get; set; }

4: publicstring FirstName { get; set; }

5: publicstring LastName { get; set; }

6: }

The next thing you’ll see is a resource class called “MyModelResource”. Again, you should use VS.NET refactoring to rename it to something that is sensible for your application. I’ll rename it to “ContactResource” (this is a similar approach to the starter code in the REST Start Kit from a couple years ago – of course they didn’t have NuGet at their disposal!). The code looks like this:

There are several things to note about this code. First, I’m just using a static Dictionary for temporary storage. This can be replaced with your “real” repository of choice. The Get() method returns an HttpResponseMessage which is the strongly typed HTTP message available in the Web API. You can see that if the id is not found, we’re able to throw an HttpResponseException which is a much more intuitive way of dealing with exceptions than what we have today. I’m also able to set expiration headers – all without referring to the static WebOperationContext. The other methods are similarly easy to work with. It’s important to point out that this is not a RESTful service – this is merely a CRUD service over HTTP. Although I do set the location header in the Post() method, I’m not leveraging hypermedia and therefore, not RESTful.

At this point, all I have is a single C# file – how do I get this hooked into the WCF pipeline? The next thing I take advantage of is the WebActivator NuGet package. I follow the established pattern for the “App_Start” folder and put my initialization code in here. This code will automatically be invoked when the web site starts up (no need for Global.asax). Then just change the first URI segment from “MyModel” to whatever you want. I’ll change it to “Contacts”:

A logical next step would be to create a NuGet sample package that contains starter code for more advanced features such as IoC, media type formatters, etc. The Web API is getting better and better. It’s now easier and simpler than ever to work with HTTP. Now is the time to start getting familiar with it and provide your feedback to the team.