Using HttpHandlers with mod_mono
Mike Roberts’ post about disposing with Web Forms piqued my curiosity about HttpHandlers. I knew what they were, but I’d never considered using them over Web Forms for standard web pages.
As Mike says, you lose all the power (and complexity) that System.Web.UI.Page provides but that could be taken as both a good and/or bad thing.
Not content with just playing with a new feature, I decided to do it on Mono and based on the various instructions I’ve seen for ASP.NET/IIS usage of HttpHandlers, they are essentially the same. Speaks volumes for the Mono project.
Here I’ll explain the steps you should follow to repeat my experiment.
First We’ll write the handler, then we’ll configure ASP.NET to use it, then we’ll configure Apache to handle it correctly, then finally we’ll see it in action.
Step 1 – Writing the handler
HttpHandlers are classes that implement the IHttpHandler or IHttpAsyncHandler interfaces. For this exercise we’re only interested in IHttpHandler, and for that we need to implement the Public IsReusable property (typically True) and ProcessRequest method.
Here’s the code for our handler (SimpleHandler.cs):
using System;
using System.Web;
public class SimpleHandler : IHttpHandler
{
public void ProcessRequest(HttpContext Context)
{
Context.Response.Write("Hello World!");
}
public bool IsReusable
{
get{ return true; }
}
}
Simple, eh? Now compile it with:
$mcs -t:library SimpleHandler.cs
Copy the resulting file (SimpleHandler.dll) to your bin folder within your web application.
Configuring ASP.NET
We’ll need to edit our web.config to handle our handler. First step is to decide what sort of pages (.html,.aspx etc.) we’re going to handle. Since our handler is called SimpleHandler, let’s handle .simple files. Here’s a complete web.config to handle this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.simple" type="SimpleHandler,SimpleHandler" />
</httpHandlers>
</system.web>
</configuration>
See here for an explanation of the <httpHandlers> element.
Configuring Apache
OK, we’ve got our handler, and we’ve configured ASP.NET to use our handler for pages ending with .simple extension. Now we need to instruct Apache1 to use mod_mono for .simple pages. Simply add the following to your Apache configuration file:
AddHandler mono .simple
Restart Apache, and browse to any .simple page (e.g. http://server/foo.simple). Because of the way we’ve configured it, any page requested with a .simple extension will be handled by our handler.
All talk, no action
Not true at all. Click here or here to see it in action.
Of course we can do something more impressive than that!
I’ve set up a second handler – PageHandler – which handles .page pages. It uses template4dotnet to provide the static content/formatting, and the handler provides the dynamic content. Go look.
I would need to do some performance testing, but I would definitely consider using HttpHandlers (with some form of templating system) for creating sites. There’s still very much a place for Web Forms, but now I recognise they’re just one facet of ASP.NET.
1 Although IIS has a different way of doing it, you’re still performing the same action – instructing the web server to use a specific program to process specific page types.
Blogmap
I’ve been doing some research into the MapPoint web service recently, including attending a webcast on the subject. The service itself looks quite impressive, but I’m not sure how the fees would stack up. For a commercial organisation, you could produce some seriously impressive integrated applications – the example from the webcast was for a claims adjuster application that would obtain the agent’s scheduled calls for the day from the company web service, then obtain directions from the MapPoint web service and display them in an AutoRoute style. Cool demo, and I can think of lots of uses for a similar application.
Services like this aren’t free, and that’s a reality you have to face if you want quality of data. Although there will be ‘free’ efforts out there (Jono wrote about OpenStreetmap a while ago but I’m not sure how they’re getting on) I can’t imagine them being able to produce the same quality of data in the foreseeable future. Mapping costs large amounts of money, and even services like MapPoint and Google Maps (which is a damn cool application by the way) license their maps from mapping companies.
We’ll just have wait and see if Google opens up and API for their map service, but as they’re re-distributing other peoples content rather than their own it’ll be interesting to see how they make it pay.
Blogmap
Via. Jamie Cansdale
The map shown above is generated by Blogmap, and shows the ‘home’ of this site. Blogmaps is (obviously) built on top of MapPoint, and demonstrates the power (and flexibility) of the service. It looks like it could be a fun service if enough people register with it, especially the ‘Local Blogroll’ feature.
Hoary Beagle Hat
Been playing a bit recently.
Tried upgrading to Ubuntu Hoary, but broke it. ![]()
Replaced broken Hoary with Fedora Core 3.
Quickly replaced Fedora with Hoary – this time installed correctly.
Fedora Core 3
I departed the Fedora world last year when I found Ubuntu, not long after I’d upgraded to FC2. Then I had to spend a while configuring GNOME to look like GNOME should (i.e. not the BlueCurve style). It was nice to see FC3 GNOME is no longer configured this way. Unfortunately, after experiencing apt-get on Ubuntu, yum (and even apt4rpm) feel a poor substitute.
Final nail in the coffin? There aren’t any ‘official’ (either packaged by Fedora or Ximian/Novell) Mono packages for FC3.
Ubuntu Hoary Hedgehog
Note to self – using dist-upgrade instead of upgrade means that things go so much smoother…
Hoary looks very slick. Having a few minor niggles (can’t edit signatures in Evolution, can’t synchronise appointments/task from my Palm Vx), but on the whole it’s good.
It also has up-to-date Mono packages in universe as well.
Beagle
Inspired by Jono I decided to install Beagle. Following the instructions worked without fault, and I’ve not had the stability issues Jono mentions either.
Very impressive app – I’ll have to try Dashboard next.
GTK#
I’ve also been playing with GTK# development on Microsoft Windows via Paco’s GTK# Installers for .NET. Although not as easy as Windows Forms development (mainly due to the lack of integration with the IDE), something about GTK+ seems more intuitive. It requires much more thought to wire things up correctly, especially if you are using Glade, but that’s not necessarily a bad thing.
I’ve not yet got GTK# 1.9.1 running on Ubuntu, but recompiling my app worked fine, even referencing .NET compiled assemblies which had just been copied across.
All-in-all, a productive few days.
Hedgehog Hat
Been playing a bit recently.
Tried upgrading to Ubuntu Hoary, but broke it. ![]()
Replaced broken Hoary with Fedora Core 3.
Quickly replaced Fedora with Hoary – this time installed correctly.
Fedora Core 3
I departed the Fedora world last year when I found Ubuntu, not long after I’d upgraded to FC2. Then I had to spend a while configuring GNOME to look like GNOME should (i.e. not the BlueCurve style). It was nice to see FC3 GNOME is no longer configured this way. Unfortunately, after experiencing apt-get on Ubuntu, yum (and even apt4rpm) feel a poor substitute.
Final nail in the coffin? There aren’t any ‘official’ (either packaged by Fedora or Ximian/Novell)
Gaining Acceptance?
The other day I attended a webcast on Mono: Running .NET Applications on Linux presented by MSDN.
That’s right – Microsoft giving floor (or rather web) space to Mono.
It was presented as part of MSDN’s Interop month, and the focus of the webcast was – as the title says – running .NET applications on Linux, although he did show some demos going the other way. Although I didn’t really learn anything from the webcast (nor did I expect to) it was nice to see the Mono VB Compiler in action. There was some coverage of using System.Windows.Forms, but it was using the old Wine-based implementation and not the newer (and far more impressive) Managed.Windows.Forms. There’s another one coming up next week – Mono: Interoperating with Linux the Easy Way.
The webcast was given by Joe Hummel who I’ve since exchanged a few emails with about how the next webcast could be improved and possible future ones as well, and he seems like a nice guy.
Virtual On
During the webcast, Joe was running Red Hat 9 within Virtual PC 2004, a setup I’d created myself a few days before – although mine was running Ubuntu of course.
Although it does highlight the inadequacies of my current desktop, it’s a very usable solution (apart from the known colour depth issue) which I’ve since expanded to included a 2nd Ubuntu install which I then upgraded to Hoary. Next I’m planning on adding a Novell Linux Desktop install to the collection.
My virtual adventures were prompted by some posts by Peter Provost on developing in virtual machines. This was an idea I’d been toying with for quite a while and it’s nice to finally put it into action. Eventually I would like to replace the host OS (XP) with Linux, but it’s a matter of finding good-enough, cheap-enough (QEMU vs. VMware) virtualisation software with the added factor of laptop support in the host OS – which of course Linux is notoriously poor for…
Until then, XP & VPC combined are enabling me to do far more than any single OS.
As seen on…
On a semi (Mono) related note, Nat Friedman has published some demos showing Beagle and Mono in action. Very cool, and I may have a go at producing some myself based on the web services I posted about before.
look good