I have been looking thought the latest release of the WCF REST Starter Kit and there are some interesting things there. Especially the new HttpClient class in the Microsoft.Http namespace. I will not talk about the server side of things because I really don’t have anything nice to say and until I have some constructive criticism to give I’m going to keep my mouth shut.
However, the new client side library is designed to make it easier to talk to HTTP endpoints and it seems to be quite nice. It is 3500 lines of code, so there is a fair bit of stuff in there, layered on top of HttpWebRequest and I was curious to see what it could do.
I started by looking at the enclosed sample, unhelpfully named “WpfSample” and I got a little sidetracked from my exploration. This sample is actually a very neat little http request builder tool. I know this is a cool tool to have, because I have an almost identical one that we built to help us test REST services. Testing with real web browsers is really annoying because they do all sorts of magic stuff behind the scenes, they are very picky about what content-types they will render and they keep trying to find a favicon!
Anyway, I was curious to see how this tool made use of the HttpClient class but I was rather dismayed to see all of the code in the “code behind” of the Window1.xaml file. This makes it really difficult to distinguish between what code is relevant to the web request and what is just UI gunk. To add salt to the wound, it is a WPF app that uses no data binding and no Commands, just events manipulating the contents of UI elements. It was a nice looking, handy WPF app that demonstrated this new spiffy HttpClient library with the architecture of a college student’s VB4 project.
Ok, I know it is just a sample. However, everything that Microsoft puts out is viewed by people who are learning. If Microsoft create samples with crappy architecture, why are we surprised when developers create applications with crappy architectures.
So despite the ton of work that I was supposed to be doing today, I refactored it. I created an MVC type of structure with databinding and routed commands. If you get the WCF REST Starter Kit and extract the zip file that gets put in the “C:Program Files (x86)Microsoft WCF REST” folder you will find a solution named Product.sln. Just add my project which is in the zip file below and you will have, what I hope is a much easier to read version of the HttpClient sample.