views:

55

answers:

1

When moving to MVC, and now IIS7, we started having issues in that our HTTP Module that opens and closes the ISession was called on every request (static files, etc.). I wanted to avoid doing a full rewrite of NH session management, so I implemented this code in my module, to filter out everything but requests going to the mvchandler:

void context_PreRequestHandlerExecute(object sender, System.EventArgs e)
{
    HttpContext context = ((HttpApplication)sender).Context;
    Type mvcht = typeof(System.Web.Mvc.MvcHandler);
    if (context.Handler != null && context.Handler.GetType().IsAssignableFrom(mvcht))
        {
             // Code Here
        }
}

My question is, I have never used this event in the request pipline. So, are there any hidden pitfalls in doing this? Also, am I looking at a performance issue in running this check for every request? I haven't noticed anything yet, but this is a new and still small app.

A: 

Although this doesn't specifically address your question, it should be noted that the cost of opening a session is very minimal. So you may consider not even performing this check in the first place.

DanP
True, but what pushed me over the edge was that I would randomly get a null ref exception on the current session when the end_request event would fire off for some, non MVC, request.
CocoB
In that case, I would have a look at: http://blogs.iis.net/thomad/archive/2006/11/04/precondition-what.aspx - you may be able to handle this purely through config of the module.
DanP