In the process of upgrading my IIS 6 to IIS 7, my website was down for almost a whole day. The problem, at first, was that only front page of the site worked all other pages returned a status 404 due to the fact that all routing was stopped working. The problem which was fixed by my hosting provider, but after that, the whole web site stopped working.

I was using NHibernate and now IIS 7, so why wouldn’t this work? I got a strange exception as soon as the request was processed by IIS engine:

1
2
3
4
5
6
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineStepManager.ResumeSteps(Exception error) +929
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context,
AsyncCallback cb) +91
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,
HttpContext context) +508

and the stack trace gives no clue. After a more close look at the application start, I found the problem to be initialization of NHibernate at application runtime. I used to do this:

1
2
3
4
5
6
7
protected override void OnStart()
{
base.OnStart();

RegisterSessionFactory();
RegisterSerivces();
}

And it seems IIS 7 now complains when I do this! I need to relocated this pieces of code to somewhere else, and also need to make sure it runs only once. Naturally I chose the next best thing: BeginRequest event.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public MvcApplication()
{
BeginRequest += OnBeginRequest;
EndRequest += OnEndRequest;
}

private void OnBeginRequest(object sender, EventArgs e)
{
if (SessionFactory == null)
{
RegisterSessionFactory();
RegisterSerivces();
}
}

This time, I was facing another exception, but the good thing is that means progress. The error message reads: “Event handlers can only be bound to HttpApplication events during IHttpModule initialization.”. To make this work, I just moved the event subscription code to Init method:

1
2
3
4
5
6
7
public override void Init()
{
base.Init();

BeginRequest += OnBeginRequest;
EndRequest += OnEndRequest;
}

Everything is now back to normal, but the lesson learned was that check the site thoroughly when a change is made, even if the change looks innocent and small.