A New Web for .NET: WCF Web APIs

If the sight of “WCF” in the title makes you want to click your back button, stop. Just wait. I tell you that I have been there, too. This isn’t that WCF. Well, it is, sort of. If you listened to our last several podcasts, you heard Glenn Block talk about his transition from MEF to the WCF team and that he was going to be working on REST. Well, it’s here. Before you continue reading, I recommend you:

  1. Download the bits
  2. Read Glenn’s posts (yes, there is a link in that last one on how to wire up the Razor view engine)
  3. Watch the RESTFest keynote and video

I know, That’s probably asking a lot. I promise, you won’t be disappointed. The WCF team has done a bang up job on this, and it’s continuing to improve. For my part, I’ve been working on making the transition to F# smoother.

Why am I excited about this? I see the new Web APIs as a fantastic, Rack+Sinatra-like platform for .NET. No, it’s not exactly the same, but it fulfills a very similar function. The new Web APIs give you a to-the-metal experience, with control over everything in a statically-typed environment. (Yes, even the headers!) A lot of the WCF abstraction has been stripped away so that HTTP, the application protocol, is available in its fullness.

“Sounds great,” you say, but I already have MVC for that. True, and yes, you can do JSON services with MVC. However, HTTP is primarily a messaging protocol, which WCF fits nicely. MVC is primarily an abstraction for building web pages in HTML. So again, yes there is overlap. Give it a whirl, however, and you’ll probably start seeing, as so many in the Ruby community, that the simpler abstraction offered by the WCF Web APIs is more efficient for REST services and even web application environments. (More on that last part towards the end of the series.)

Simple, yes, but you can already see the same power offered by Rack. Middleware is just a step around the corner. A simple Sinatra-like DSL would make this trivially easy to compose application parts. The WCF Web APIs will offer a composition mechanism, though that is still in the works.

What do I mean? Well, let’s look at a trivial example:

For completeness, here’s the FuncHost wrapper and other type declarations I’m using:

Go try it out. Let the team know what you think! If you want to show off what you are doing, consider sending a pull request to http://wcfhttpcontrib.codeplex.com/, where we’ll be adding middleware and other common components for your convenience. More on middleware next time.