Registering Custom HttpModule in IIS7 Web.config

I’ve been writing an Error Hander HttpModule for a current ASP.NET WebForm project and things were going well until my last merge with TFS. All of a sudden, my HttpModule wouldn’t register anymore.

For the life of me I couldn't’ figure out what had changed. I spent half the day trying to figure out what in the world was going on.

Here is the chain of events that I tried before finding the actual solution. If you want, you can just skip to the end to find the answer :)

First, I decided to strongly sign the assembly with the HttpModule (even thought it wasn’t necessary previously)…

So, I created a new Strong Name Key from the properties window of my HttpModule project from Visual Studio 2008

CreateKey

Then, in order for me to get the Public Key Token to decorate the HttpModule entry in the web.config, I used the following command line tool
sn –T ErrorFramework.dll

Note: You might consider integrating this command into a Get Public Key Token External Tool in Visual Studio 2008.

Here is what my original web.config entry looked like before:

 

and after all of the above steps I was able to update my web.config to the following:

      

However, to my dismay the HttpModule still did not register!

Secondly, I tried dynamically registering my HttpModule in the Global.asax instead of relying upon the web.config.

namespace ErrorFramework
{
public class Global : System.Web.HttpApplication
{
public static ExceptionModule errorModule = new ExceptionModule();

public override void Init()
{
base.Init();
exceptionModule.Init(this);
}
}
}

This thankfully worked fine, but I really wanted the web.config option to work so that I could add or remove the HttpModule at will without having to change and recompile code.

Thirdly, I decided to use Visual Studio’s internal webserver (Cassini).

To my surprise the HttpModule started to work again! Although, I was excited that it worked… I was also very confused because I thought it should work through IIS7 as well. So, back to the drawing board.

Fourthly, I finally found the answer I was looking for.

Apparently, IIS7 looks in the system.webSever/modules section of the web.config and not in the system.web/httpModules section like IIS5 & IIS6. It turns out that the web.config that our project has both sections defined in the config file!

So, instead of this…







I needed to do this…







Note: If you want it to work both on IIS7 and through Cassini, then you’ll need to define it in both places ;)