title: "Session is Closed!" - NHibernate
body: <p>This is in a web application environment:</p>
  
  <p>An initial request is able to successfully complete, however any additional requests return a "Session is Closed" response from the NHibernate framework. I'm using a HttpModule approach with the following code:</p>
  
  <pre><code>public class MyHttpModule : IHttpModule
  {
      public void Init(HttpApplication context)
      {
          context.EndRequest += ApplicationEndRequest;
          context.BeginRequest += ApplicationBeginRequest;
      }
  
      public void ApplicationBeginRequest(object sender, EventArgs e)
      {
          CurrentSessionContext.Bind(SessionFactory.Instance.OpenSession());
      }
  
      public void ApplicationEndRequest(object sender, EventArgs e)
      {
          ISession currentSession = CurrentSessionContext.Unbind(
              SessionFactory.Instance);
  
          currentSession.Dispose();
      }
  
      public void Dispose() { }
  }
  </code></pre>
  
  <p>SessionFactory.Instance is my singleton implementation, using FluentNHibernate to return an ISessionFactory object.</p>
  
  <p>In my repository class, I attempt to use the following syntax:</p>
  
  <pre><code>public class MyObjectRepository : IMyObjectRepository
  {
      public MyObject GetByID(int id)
      {
          using (ISession session = SessionFactory.Instance.GetCurrentSession())
              return session.Get&lt;MyObject&gt;(id);
      }
  }
  </code></pre>
  
  <p>This allows code in the application to be called as such:</p>
  
  <pre><code>IMyObjectRepository repo = new MyObjectRepository();
  MyObject obj = repo.GetByID(1);
  </code></pre>
  
  <p>I have a suspicion my repository code is to blame, but I'm not 100% sure on the actual implementation I should be using.</p>
  
  <p>I found a similar issue on SO <a href="https://stackoverflow.com/questions/1103837/nh-request-per-session-session-is-closed">here</a>. I too am using WebSessionContext in my implementation, however, no solution was provided other than writing a custom SessionManager. For simple CRUD operations, is a custom session provider required apart from the built in tools(ie WebSessionContext)?</p>