title: NHibernate Current Session Context Problem
body: <p>I've recently moved from using an ISession directly to a wrapped ISession, Unit-of-Work type pattern. </p>
  
  <p>I used to test this using SQL Lite (in-memory). I've got a simple helper class that configures my SessionFactory, created an ISession and then built the schema using SchemaExport and then returned my ISession and the schema lived until I closed the session. I've changed this slightly so that I now configure a SessionFactory, create an ISession, build the schema, and pass the factory to my NHibernateUnitOfWork and return that to my test.</p>
  
  <pre><code>var databaseConfiguration =
                  SQLiteConfiguration.Standard.InMemory()
                  .Raw("connection.release_mode", "on_close")
                  .Raw("hibernate.generate_statistics", "true");
  
              var config = Fluently.Configure().Database(databaseConfiguration).Mappings(
                  m =&gt;
                  {
                      foreach (var assembly in assemblies)
                      {
                          m.FluentMappings.AddFromAssembly(assembly);
                          m.HbmMappings.AddFromAssembly(assembly);
                      }
                  });
  
              Configuration localConfig = null;
              config.ExposeConfiguration(x =&gt;
                  {
                      x.SetProperty("current_session_context_class", "thread_static"); // typeof(UnitTestCurrentSessionContext).FullName);
                      localConfig = x;
                  });
  
              var factory = config.BuildSessionFactory();
              ISession session = null;
  
              if (openSessionFunction != null)
              {
                  session = openSessionFunction(factory);
              }
  
              new SchemaExport(localConfig).Execute(false, true, false, session.Connection, null);
  
              UnitTestCurrentSessionContext.SetSession(session);
  
              var unitOfWork = new NHibernateUnitOfWork(factory, new NHibernateUTCDateTimeInterceptor());
              return unitOfWork;
  </code></pre>
  
  <p>Internally, NHibernateUnitOfWork needs to get the ISession which was used to create the schema or the in-memory database won't actually have a schema, so this is the method it calls to get the ISession.</p>
  
  <pre><code>private ISession GetCurrentOrNewSession()
          {
              if (this.currentSession != null)
              {
                  return this.currentSession;
              }
  
              lock (this)
              {
                  if (this.currentSession == null)
                  {
                      // get an existing session if there is one, otherwise create one
                      ISession session;
                      try
                      {
                          session = this.sessionFactory.GetCurrentSession();
                      }
                      catch (Exception ex)
                      {
                          Debug.Write(ex.Message);
                          session = this.sessionFactory.OpenSession(this.dateTimeInterceptor);
                      }
  
                      this.currentSession = session;
                      this.currentSession.FlushMode = FlushMode.Never;
                  }
              }
  </code></pre>
  
  <p>The problem is that <code>this.sessionFactory.GetCurrentSession</code> always throws an exception saying that the <code>ICurrentSessionContext</code> isn't registered. </p>
  
  <p>I've tried loads of different ways to set the property and different values (as you can see above, "thread_static" and my own <code>ICurrentSessionContext</code>) but none seem to work.</p>
  
  <p>Anyone got any advice</p>