HttpModules - Why using HttpApplication is a bad idea

I was using Darksider's page optimisation module to clean up and optimise my sites HTML gamerlobby.net

This was working like an absolute champ up until the night I decided to go live. Leaving ELMAH for the last minute as it was so darn easy to implement, I started to struggle to get it to start logging.
It made no sense. Database created, Connection String present and correct, modules and handlers referenced. WHAT CAN IT BE.

So after hours of scratching my head and freaking out, I commented out Darksider's page optimisation module and voila ... Logging works again.

Very saddened by the fact my pages are no longer optimised, I was determined to work this one out. So finally today, I found the bug.
The page optimisation was utilising a using statement around the HttpApplication context. This in turn was swallowing the Context and breaking any other modules that attached themselves to HttpApplication events.

So we changed this:

using (var app = sender as HttpApplication)
{
   if (app.Context.CurrentHandler is Page)
   {
     app.Response.Filter = new PageCleanFilter(app.Response.Filter);
   }
}

To this:

var app = sender as HttpApplication;
if (app.Context.CurrentHandler is Page)
{
  app.Response.Filter = new PageCleanFilter(app.Response.Filter);
}

 

Now all is logging again, and my page optimised!!
I'll be sure to pass this onto Darksider to update his post.

posted @ Monday, September 08, 2008 1:10 PM

Print
«February»
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910