title: Problem with StructureMap/nhibernate setup:
body: <p>I keep getting the following error:</p>
  
  <p>Cannot access a disposed object.
  Object name: 'AdoTransaction'. </p>
  
  <p>The setup follows the example given at <a href="http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html" rel="nofollow noreferrer">http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html</a> </p>
  
  <p>here is the IUnitOfWork class (exactly the same as the one in the link):</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
      private readonly ISessionFactory _sessionFactory;
      private readonly ITransaction _transaction;
  
      public UnitOfWork(ISessionFactory sessionFactory)
      {
          _sessionFactory = sessionFactory;
          CurrentSession = _sessionFactory.OpenSession();
          _transaction = CurrentSession.BeginTransaction();
      }
  
      public ISession CurrentSession { get; private set; }
  
      public void Dispose()
      {
          CurrentSession.Close();
          CurrentSession = null;
      }
  
      public void Commit()
      {
          _transaction.Commit();
      }
  
  }
  </code></pre>
  
  <p>here is the NHibernateModule (again exactly the same!):</p>
  
  <pre><code>public class NHibernateModule : IHttpModule, IDisposable
  {
      private IUnitOfWork _unitOfWork;
  
      public void Init(HttpApplication context)
      {
          context.BeginRequest += ContextBeginRequest;
          context.EndRequest += ContextEndRequest;
      }
  
      private void ContextBeginRequest(object sender, EventArgs e)
      {
          _unitOfWork = ObjectFactory.GetInstance&lt;IUnitOfWork&gt;();
      }
  
      private void ContextEndRequest(object sender, EventArgs e)
      {
          Dispose();
      }
  
      public void Dispose()
      {
          if (_unitOfWork == null) return;
          _unitOfWork.Commit();
          _unitOfWork.Dispose();
      }
  }
  </code></pre>
  
  <p>here is my repo:</p>
  
  <pre><code> public class Repository&lt;T&gt; : IRepository&lt;T&gt;
  {
      public readonly IUnitOfWork _uow;
  
      public Repository(IUnitOfWork uow)
      {
          _uow = uow;
      }
  
      public Repository()
      {
  
      }
  
      #region IRepository&lt;T&gt; Members
  
      public IList&lt;T&gt; GetAll()
      {
          using (var session = _uow.CurrentSession)
          {
              return session.CreateCriteria(typeof (T)).List&lt;T&gt;();
          }
      }
  
      public IList&lt;T&gt; FindAll&lt;T&gt;(IList&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; criteria)
      {
          var session = _uow.CurrentSession;
  
          var query = from item in session.SessionFactory.OpenSession().Query&lt;T&gt;()
                                select item;
          foreach (var criterion in criteria)
          {
              query = query.Where(criterion);
          }
          return query.ToList();
      }
  
      public T FindFirst&lt;T&gt;(IList&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; criteria)
      {
  
          var col = FindAll(criteria);
  
          if (col.Count &gt; 0)
          {
              return col.First();
          }
          else
          {
              return default(T);
          }
      }
  
      public T Get(int id)
      {
          using (var session = _uow.CurrentSession)
          {
              return session.Get&lt;T&gt;(id);
          }
      }
  
      public void Save(T entity)
      {
          using (var session = _uow.CurrentSession)
          {
              session.Save(entity);
          }
      }
  
      public void Update(T entity)
      {
          using (var session = _uow.CurrentSession)
          {
              session.Update(entity);
              session.Flush();
          }
      }
  
      #endregion
  }
  }
  </code></pre>
  
  <p>here is my bootstrapper:</p>
  
  <pre><code> public class BootStrapper : IBootstrapper
  {
      private static bool _hasStarted;
  
      public virtual void BootstrapStructureMap()
      {
          ObjectFactory.Initialize(x =&gt;
          {
              x.Scan(s =&gt;
              {
                  s.TheCallingAssembly();
                  s.AssemblyContainingType&lt;User&gt;();
                  s.AssemblyContainingType&lt;UserRepository&gt;();
                  s.AssemblyContainingType&lt;NHibernateRegistry&gt;();
                  s.LookForRegistries();
              });
  
              // Repositories
              x.For&lt;WmcStar.Data.IUserRepository&gt;()
                  .CacheBy(InstanceScope.HttpContext)
                  .TheDefault.Is.OfConcreteType&lt;UserRepository&gt;();
  
              x.For&lt;IDatabaseBuilder&gt;().TheDefaultIsConcreteType&lt;DatabaseBuilder&gt;(); 
  
          });
      }
  
      public static void Restart()
      {
          if (_hasStarted)
          {
              ObjectFactory.ResetDefaults();
          }
          else
          {
              Bootstrap();
              _hasStarted = true;
          }
      }
  
      public static void Bootstrap()
      {
          new BootStrapper().BootstrapStructureMap();
      }
  
  }
  </code></pre>
  
  <p>here is my NHibernateRegistry:</p>
  
  <pre><code>public class NHibernateRegistry : Registry
  {
      public NHibernateRegistry()
      {
          var cfg = new Configuration()
               .SetProperty(NHibernate.Cfg.Environment.ReleaseConnections, "on_close")
               .SetProperty(NHibernate.Cfg.Environment.Dialect, typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName)
               .SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName)
               .SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"my connstring")
               .SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
               .AddAssembly(typeof(User).Assembly);
  
          var sessionFactory = cfg.BuildSessionFactory();
  
          For&lt;Configuration&gt;().Singleton().Use(cfg);
  
          For&lt;ISessionFactory&gt;().Singleton().Use(sessionFactory);
  
  
          For&lt;ISession&gt;().HybridHttpOrThreadLocalScoped()
              .Use(ctx =&gt; ctx.GetInstance&lt;ISessionFactory&gt;().OpenSession());
  
          For&lt;IUnitOfWork&gt;().HybridHttpOrThreadLocalScoped()
              .Use&lt;UnitOfWork&gt;();
  
          For&lt;IDatabaseBuilder&gt;().Use&lt;DatabaseBuilder&gt;();
          SetAllProperties(x =&gt; x.OfType&lt;IUnitOfWork&gt;());
      }
  }
  </code></pre>
  
  <p>and finaly here is my global.asax:</p>
  
  <pre><code>    public class Global : System.Web.HttpApplication
  {
  
      protected void Application_Start(object sender, EventArgs e)
      {
          BootStrapper.Bootstrap();
          new SchemaExport(ObjectFactory.GetInstance&lt;Configuration&gt;()).Execute(false, true, false);
          ObjectFactory.GetInstance&lt;IDatabaseBuilder&gt;().RebuildDatabase();
  
          AutoMapper.Mapper.CreateMap&lt;WmcStar.Model.User, WmcStar.Data.Dto.User&gt;();
      }
  }
  </code></pre>
  
  <p>Anyone got any clues as to what would cause this?</p>
  
  <p>w://</p>