jmw
jmw

Reputation: 2864

Nhibernate with Asp.net WebForms - Session per Request

I am using an HttpModule to open a session that span each request and it works great with lazy loading and everything.

My concern is that since I open a new session per request (and stores it in HttpContext.Current.Items) it opens a session for every request even request including request for .css files and images. I recall reading that session creation is a pretty lightwheigt operation (dont know about transactions though) but anyway it seems unnecessary to open a session for a requests for a .css file?

Anyone got some ideas about this, is it a problem and/or am I doing something stupid?

Thanks in advance

Upvotes: 0

Views: 776

Answers (1)

devio
devio

Reputation: 37205

  • only create the session object if the file type is .aspx or .ashx:

        switch (context.Request.CurrentExecutionFilePathExtension.ToLower())
        {
            case ".aspx":
            case ".ashx":
                context.Items[ContextKey] = CreateMySession();
                break;
        }
    
  • or encapsulate session creation inside a property getter, and clean-up checks whether session != null

Upvotes: 8

Related Questions