The opinions expressed herein are my own personal opinions. They are not necessarily fact or sactioned by any other person or organization. If you disagree that's your right. It's also my right to not care.
© Copyright 2010, Chris Tacke
I've spent a bit of time lately adding HttpHandler support to our Padarn Web Server for Windows CE, and today I just wrapped up the code for a pretty in-depth example for using it to host RESTful Web Services. I'm going to give some highlights on how it works here in my blog as well, because while it's remarkably easy to get working, there's still a lot going on.
Background
The general idea is to have a database on a Windows CE device and a client application on the desktop that could access that database via a Web Service hosted on the device. I want to be able to do all of the CRUD operations through the service, and nicely these to the HTTP methods:
POST - Create a new entity in the databasePUT - Update an existing entity in the databaseGET - Read an entity (or list of entities) from the databaseDELETE - Delete an entity from the database
To make things simple, I created a very simple database that contains only one table. The simple data model isn't real-world, but if you can make it work for one table, making it work for your entire database is really just an extension of the code base.
Setting Up Padarn
I'm going to use custom HttpHandlers to act as the service entry points. HttpHandlers in Padarn look and work just like they do under IIS, the only difference is how they get "installed:. For Padarn, the handlers are configured, like everything else, in Padarn's configuration file. To make my life simpler, and the code easier to read, I'll create a separate handler for each separate HTTP method, so one for GET, one for POST and so on. This is how they get registered with the server:
<httpHandlers> <assembly>SampleSite.dll</assembly> <add verb="GET" path="/*" type="SampleSite.Handlers.GetHandler, SampleSite"/> <add verb="PUT" path="/*" type="SampleSite.Handlers.PutHandler, SampleSite"/> <add verb="POST" path="/*" type="SampleSite.Handlers.PostHandler, SampleSite"/> <add verb="DELETE" path="/*" type="SampleSite.Handlers.DeleteHandler, SampleSite"/></httpHandlers>
Pretty simple. the "add" nodes simply say what method/verb in any given server path maps to a given class. Padarn uses regex to parse the path, so the first add line basically says "for GET methods to any path on the server, call into an instance of a GetHandler class". Since the GetHandler is in a separate assembly from my hosting executable, Padarn needs to know about that assembly so it can load it at run time. The assembly node simply tells Padarn to load up that assembly before it tries creating any custom handler class instances.
Once this is configured, the only thing left to do is to implement the handlers. An HttpHandler is really simple - it contains a single overriden method that gets called when a page is requested. Here's what the GetHandler stub looks like:
namespace SampleSite.Handlers{ public class GetHandler : BaseHandler { public override void ProcessRequest(HttpContext context) { // do stuff here } }}
I get in an HttpContext, which has both the HttpRequest and HttpResponse objects. Note how we're trying very hard to mirror the object model Microsoft uses to make the learning curve much easier and to make your code more portable. The HttpRequest contains, among other things, the incoming path and data. The HttpResponse is where we write back out our data.
SImple enough so far, right? Next up: getting data from our RESTful service.
Remember Me