Hosting RESTful Web Services in Windows CE – Part I

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.


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 database
PUT – Update an existing entity in the database
GET – Read an entity (or list of entities) from the database
DELETE – 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:

<?xml version=”1.0″ encoding=”utf-8″ ?>
  <WebServer …>
    <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”/>

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 overridden 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.

4 thoughts on “Hosting RESTful Web Services in Windows CE – Part I”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s