MVC and NHibernate problem with IIS 7
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 | [ ] |
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 | protected override void OnStart() |
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 | public MvcApplication() |
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 | public override void Init() |
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.