Microsoft WCF gets serious about HTTP

Chances are, if you have read any of my blog posts before, you will probably be aware of my appreciation of a certain HTTPClient library that appeared in the WCF REST Starter kit a few years ago.  After a very long incubation period and some periods of uncertainty about its future I am really excited by the fact that a official .Net 4 version is now downloadable from the new Codeplex site http://wcf.codeplex.com

When I started the articles on HttpClient library I had planned to write a piece on how HTTPClient can be used on the server.  I never did get around to it, and I’m kind of glad I didn’t spend any time on it, because now I have something much better to write about, Microsoft.ServiceModel.Http.

PDC 2010

In Glenn’s talk at PDC today he announced an evolution of WCF’s approach to dealing with the web.  This new set of libraries are built on the foundations of WCF but with a different perspective.  Don’t abstract the application protocol, embrace it.  If there was a mantra to this project, it was “let’s do HTTP right, the way RFC2616 says it should be done.”  At the core, the WCF engine it is still powering the bytes across the wire, because, for all the things I have cursed WCF for in the past, performance and reliability have never been issues for me.  It is one solid communication library.  

But what about all the other Http offerings

I am sure many people are going to ask, but what about System.ServiceModel.Web, isn’t that WCF’s solution to HTTP and REST?   What about WCF Data Services? OData? Why WCF at all, why not just stick with ASP.NET MVC, it does HTTP pretty darn well. 

All of these solutions do address a certain problem space.  OData makes it really quick to expose raw data. ASP.NET MVC is great for delivering HTML and javascript.  System.ServiceModel.Web provides web services without the friction of SOAP.

However, to an extent each one of these solutions are technology silos, and they each come with their set of limitations and quirks relating to their underlying technology.    And if you believe System.ServiceModel.Web was sufficient to easily do RESTful systems, there are hundreds of people waiting for your wisdom here.

Old friends and new friends

This new WCF HTTP stack provides the building blocks that can be used to address all of these problem spaces, either as a direct replacement or as a complimentary solution.  In the first code drop on Codeplex there is a prototype project called Microsoft.ServiceModel.WebHttp that emulates the behaviour of System.ServiceModel.Web.  Other prototypes have been worked on that provide an experience closer to the way OpenRasta works.  This new infrastructure also plays very nicely with ASP.NET as you can see in the ContactManager Sample.  The ASP.NET ServiceRoutes can be used to host WCF services so that ASP.NET and WCF can work seamlessly side by side.

Although, there is currently no sample showing it, the new stuff can also be self-hosted.  I am currently putting together a sample to do that which I will be posting on http://wcfhttpcontrib.codeplex.com.

RFC2616 all the way

One of the best things about the HttpClient library was the strong types that helped me when making HTTP requests.  All the headers have strong types that deal with the string parsing details.  And because the types are consistent with RFC2616 learning them is easy if you already know HTTP.  In fact it has been quite surprising to me how much I have learned about HTTP from the types.  Microsoft.ServiceModel.Http actually has a dependency on the Microsoft.Http client library and it re-uses all those strong types.  You use the same HttpResponseMessage and HttpRequestMessage on the server as you do on the client.

Where’s the magic?

One thing I hated about WCF was the magic.  Sprinkle some attributes on your classes, scatter some Xml elements in config files and fire up your service.  When a request comes in, magically it executes your operation.  This inevitably leads to questions like, how do I implement logging across all endpoints? how do I handle exceptions?  and hundreds of others on the intricacies and limitations of how WCF deals with parameters passed to operations. 

So how does Microsoft.ServiceModel.Http attempt to address these cross-cutting concerns?  With a pipeline of course.  To be more specific a request and response pipeline for every Http Method / URI template combination.  However, the pipeline is not just there for WCF to do its thing, it is also available to the application developer to add in whatever functionality they want.  So you can make your own magic happen!

The basic idea is that you can add processors into the the request and response pipelines and each processor will be given an opportunity to contribute to the end result in some way.  Each processor is required to declare what it wants as input parameters and what it will output.  The output of the processor is accumulated in the context of the request and can be consumed by later processors.

As one example, if you consider the System.ServiceModel.WebHttp prototype that is used by the ContactManager sample. It automatically sets up a UriTemplateProcessor and an XmlProcessor on the request pipeline and a ResponseEntityBodyProcessor and an XmlProcessor on the response side.  The UriTemplateProcessor is responsible for using the URITemplate to parse parameters out of the incoming URI. The XmlProcessor is resposible for serializing and deserializing objects to XML. The ResponseEntityBodyProcessor is there to set the ContentType header of the response. 

With just a few processors sitting on top of the HTTP stack, it is possible to emulate what System.ServiceModel.Web does.  This is going to be very important for backwards compatibility, for those who have already invested in these tools, but would like to gain the flexibility of the new stuff.

Pipeline processors are going to enable us to do all sorts of cool stuff and I expect to be doing some more articles on my adventures on building processors to support the REST projects I am building.

it’s the same, but different

For people who have seen WSGI and Rack, this idea of a pipeline may sound familiar.  However, from what I understand it is different in a couple of significant ways.  First, processors are very explicit about what they accept as input parameters and what output parameters they contribute.  The second is related to the fact that WCF HTTP does not route all requests through the same pipeline.  When the service is initialized, a configuration sequence is used to identify all of the operations that exist and what processors will be used when accessing that specific endpoint. 

Initially I was horrified by the idea of of hundreds of these pipelines being built at startup but the more I become familiar with the inner workings of WCF, the more it appears to be a common trade off: do as much work as possible during initialization, to reduce the work required while processing a request.  Considering the number of times a service is started, as compared to the number of times a request is processed, I think I can live with a slower startup time and a bit of additional memory overhead.    This approach also makes it much less costly to apply a processor to only a subset of URIs as the filtering is done just once upfront.

These are just building blocks

The bits on the new WCF Codeplex site are really just the beginning.  They lay a foundation on top of which we can build a really effective REST frameworks.  I intentionally used frameworks, plural, because there are many ways to implement RESTful systems and I think opinionated frameworks are a good idea but not everyone shares the same opinions :-)

At last a reality that is not painful

So in essence, my experience with both System.ServiceModel.Http and Microsoft.Http have led me to the opinion that this is a great starting point for building distributed applications using HTTP.  The benefit of it being based on WCF technology is that it is a proven technology that plays nicely with other protocols, so when HTTP is not the right solution, you can use the same infrastructure.  And finally, we have a solution that begins the process of unifying the variety of different web technology stacks around a solid web specification, with an extensibility model that is actually comprehensible.

What next?

You will quickly discover the documentation on the Codeplex site is sparse.  I think one reason for this is the code has been changing so much over the last few months that trying to do documentation would have been futile.  However, moving forward I expect that to change pretty rapidly.  You will also notice that the docs and samples are currently heavily focused on IIS/ASP.Net hosted solutions.  Do not let that deceive you, self-hosting still rulez!  I hope to contribute towards fixing that disparity.

13 thoughts on “Microsoft WCF gets serious about HTTP

  1. Pingback: Tweets that mention Microsoft WCF gets serious about HTTP | Bizcoder -- Topsy.com

    • When you get to the core, IIS and self-hosted WCF http services are both running on Http.sys. When you say IIS has fault tolerance, I’m not exactly sure what you are referring to, but if you are talking about the worker process isolation mode then I see no advantage. Self-hosted services already run in their own process. Windows services are capable of recovery on crashes. As far as eventing, you do get some stuff out of the box. However, with Log4Net it is really easy to implement logging and just recently I implemented a secondary log for my service that generates W3C format log files so that I can use standard web analysis tools.
      Self hosted services can be installed by running an MSI file. I find that installing applications into IIS is full of administrative overhead. You have to worry about what http methods are blocked by IIS, what extensions/media type mappings exist. At any point in time, IIS can intercept the call to your service and decide that it will process it instead of you.
      Much of the problems I have with IIS are probably related to my ignorance of it, but I have had to use it casually since IIS4 and it still finds ways of making my life difficult.

  2. Pingback: WCF Web APIs, HTTP your way - The .NET Endpoint - Site Home - MSDN Blogs

  3. Pingback: WCF Web APIs, HTTP your way - Glenn Block - CodeBetter.Com - Stuff you need to Code Better!

  4. Pingback: WCF Web APIs, HTTP your way | RecklessDevelopment

  5. actually speaking wcf is sometimes very frustating as it has no proper specified reasons for its errors.It leaves the developer always confused with errors and problems even after everything works well for long !!

    could u tell me one thing please?..

    see BasicHttp does not involve callbacks and DuplexHttp does support callbacks..but Duplex ,inspite of supporting callbacks is not used most as it needs to compromise on firewalls ?? if this is so,then why would anyone use Duplex? why doesnt basicHttp support security features ?..

    are my concepts right??…wcf doubts me and I doubt it !!

    some concepts are weak of mine i feel..but there is no way to come out of these confusions as WCF is VAST and there are thousands of articles !!

    the above are my doubts which u may find it silly maybe ..!! but that is to be cleared out as well !!

    thanks a lot..your blogs are a gud one i must say, that understands what wcf developers go through !

    Mumtaz

    • All the WCF “http” bindings like BasicHttpBinding, WSHttpBinding, WSDualHttpBinding, WSFederationHttpBinding are actually using SOAP as the application protocol and HTTP simply as a transport. The confusion you are experiencing is more related to SOAP and WS-* and less to do with WCF itself.

      This new Http stack has nothing to do with those bindings at all. The webHttpBinding is closer to this stack but webHttpBinding is still tied heavily to serialized XML. The new stack is completely agnostic to the media-type you decide to use. I expect this new Http stack to completely replace webHttpBinding in the future as it has ways of being backward compatible with services based on webHttpBinding.

      Http itself does not natively support callbacks, so this will not be a feature of this stack. However, the WCF team are doing work with WebSockets so that may provide some kind of duplex solution in the future.

  6. Hi Darrel

    Reading this and Glenn’s blog posting is fascinating. A few days ago a began a discussion on LinkedIn (LIDNUG) about parts of the .NET Framework people hate – mine being WCF.

    I began to consider exactly the same stuff as what is being worked on. It’s absolutely uncanny. The abstraction hinders and the efforts to make the abstraction as flexible as the raw components just increases its surface area and multiplies the configuration options.

    Strip out the aging, irrelevant IIS. Strip away most of WCF, esp. SOAP.

    Just equip us with the tools, i.e. some conventions, a bunch of types and the pipelines to get the job done ourselves. The rest is superfluous abstraction… distraction more like.

    Leaner. Meaner.

    I’m so happy to read all this. I’m welling up*.

    Luke

    *I’m not welling up.

  7. Pingback: WCF Web APIs Roundup - Volume 1 - The .NET Endpoint - Site Home - MSDN Blogs

  8. Pingback: WCF Web APIs Roundup - BooleanBase

  9. Pingback: WCF Web APIs Roundup – Volume 1 | Glenn Block

  10. Pingback: WCF Web APIs, HTTP your way | Glenn Block

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>