Posts
208
Comments
1144
Trackbacks
51
The Time is Now to Start Checking out the WCF Web APIs

Over the last 3 years, Microsoft has slowly but surely been embracing REST as a programming model for the web in WCF. .NET 3.5 SP1 introduced some REST constructs allowing us to invoke WCF services with HTTP GET/POST/PUT/DELETE verbs in conjunction with UriTemplates. The REST Starter Kit (RSK) was then introduced on CodePlex and this provided many great features including an HTML help page, request interceptors, an HTTP client library, and more. Then WCF 4 was released and some of the features in the REST Starter Kit (e.g., automatic help page) were moved into the actual framework. At this point WCF has gotten *much* easier to work with and I have blogged in the past showing how simple it is to stand up RESTful services with no svc file and no config. This makes the experience just as easy as building services in MVC but you get richer functionality than MVC provides out of the box with automatic help page and automatic format selection (xml, json, etc.).

At PDC10 last week, the WCF web http team showed that this story is only going to get better – much better. In the presentation “Building Web APIs for the Highly Connected Web”, Glenn Block (currently a PM on the WCF team but you might know him from his work with MEF) showed numerous examples of the types of things that we can be expecting to be released in the future. And these features are available right now for you to start playing with – just head over the the WCF site on CodePlex to download the bits. Go ahead – do it now. I have the pleasure of being a member of an HTTP WCF Advisors group organized by Glenn and the one thing I can definitively tell you is that Microsoft is listening. Yes, in the past Microsoft sometimes added features to the .NET framework that we as developers didn’t always find useful or simple enough to use. But that is not the case with the WCF HTTP team – if you have a feature you are interested in, go tell them!

One of the key features you’ll find in the latest bits are Processors. When the RSK was introduced, it included RequestInterceptors which were incredibly helpful for intercepting the request to get at headers and content of the current request through the HttpRequestMessageProperty type. However, the RequestInterceptors feature did *not* make it into WCF 4. With the introduction of Processors you’ll be easily able to do everything you could do with RSK and more. One of the things that is very easy to do with Processors is to manipulate the format of your requests. For example, in WCF 4 with webHttp you get automatic format selection (between XML/JSON) by adding this to your web.config file:

   1:  <system.serviceModel>
   2:    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
   3:    <standardEndpoints>
   4:      <webHttpEndpoint>
   5:        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
   6:      </webHttpEndpoint>
   7:    </standardEndpoints>
   8:  </system.serviceModel>

Notice on line #5 above, we turn on automatic format selection. This allows us to pick either XML or JSON by specifying the appropriate value in the Accept header. However, with Processors now, you don’t even need this config – in fact, you don’t need any XML configuration at all. Instead you can create your own configuration class in C# and specify the JsonProcessor there:

   1:  public class MyConfiguration : HostConfiguration
   2:  {
   3:      public override void RegisterRequestProcessorsForOperation(HttpOperationDescription operation, IList<Processor> processors, MediaTypeProcessorMode mode)
   4:      {
   5:          processors.Add(new JsonProcessor(operation, mode));
   6:      }
   7:   
   8:      public override void RegisterResponseProcessorsForOperation(HttpOperationDescription operation, IList<Processor> processors, MediaTypeProcessorMode mode)
   9:      {
  10:          processors.Add(new JsonProcessor(operation, mode));
  11:      }
  12:  }

You can then register this configuration during application start up in your global.asax like this:

   1:  RouteTable.Routes.AddServiceRoute<FooResource>("foo/", new MyConfiguration());

There is a “Contact Manager” sample available in the download that shows this functionality. The ability to avoid XML configuration is nice but it’s really only part of the story. The real beauty of this is that, since the Processors are extensible, you can add your own processor for ANY format that you want to support from your service – e.g,. xml, json, atom pub, html, png, plain text, etc,. etc.

One of the things that I was most disappointed in when I saw what was put into WCF 4 was the lack of the HttpClient which was introduced in the RSK. Fortunately, it is now back and enhancements are actively being worked on!

Given the huge popularity of jQuery, there is also direct support for sending JSON to the server using C# dynamic typing so you can work with JSON on the server similar to dynamic languages like Ruby.

There is also much more on the horizon – much of which hasn’t even been publicly announced yet. This is the time to start checking out what Microsoft is doing to enable HTTP in WCF. Go to http://wcf.codeplex.com/ now – download the bits and tell the team what you think.

posted on Sunday, November 7, 2010 10:14 PM Print
Comments
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Roberto Hernandez
11/13/2010 3:09 PM
Good to know you have a say in the future of the HTTP WCF Stack! WCF has been managed as a ship without a pilot for a long time.
Roberto.-
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Jay
11/20/2010 2:41 PM
Exxciting!. I want to try it out. It says that There is a “Contact Manager” sample available in the download that shows this functionality. Where is the contact manager sample?
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Steve
11/20/2010 11:03 PM
@Jay - Once you download the code from the CodePlex site, you'll find the Contact Manager projected physically located at: Http\samples\ContactManager. But if you just open the solution, you'll be able to find it pretty quickly in the "samples" virtual folder.
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Alon Yaari
12/16/2010 10:59 AM
Hi,
It's great I have already start using it!
So what is replacing the RequestInterceptors .
I want to check the session(my own managed session) on a central point and if it's not valid to send response to the user with some httpstatuscode and not execute the function.I was doing it easily with the RequestInterceptors , how can I do it now?
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Steve
12/16/2010 11:07 AM
@Alon - You can do this with the Processors now. There will also be some Channel integration.
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Alon Yaari
12/21/2010 2:19 AM
Hi Steve thanks for the answer.
you mean by override OnExecute method?
how can I prevent the it to go to the real service method (in case he forgot to put some session header for example)
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Steve
12/23/2010 12:09 AM
Yes, that's what I meant. In terms of preventing it from going to the real service method, I *think* that's not supported in this release but will be in the next. For a more definitive answer, you should ask that question in the forum (http://wcf.codeplex.com/discussions/topics/4885/http) - the team is pretty responsive.
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Dmitri
5/13/2011 4:26 AM
Looks like the Processor functionality has been scrapped or renamed, right?
Gravatar
# re: The Time is Now to Start Checking out the WCF Web APIs
Steve
5/13/2011 7:23 AM
@Dmitri - Correct. Check out Handlers like HttpOperationHandler.

Post Comment

Title *
Name *
Email
Comment *  
Verification

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud