Hi
I keep getting the following error:
Cannot access a disposed object. Object name: 'AdoTransaction'.
The setup follows the example given at http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html
here is the IUnitOfWork class (exactly the same as the one in the link):
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();
}
}
here is the NHibernateModule (again exactly the same!):
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<IUnitOfWork>();
}
private void ContextEndRequest(object sender, EventArgs e)
{
Dispose();
}
public void Dispose()
{
if (_unitOfWork == null) return;
_unitOfWork.Commit();
_unitOfWork.Dispose();
}
}
here is my repo:
public class Repository<T> : IRepository<T>
{
public readonly IUnitOfWork _uow;
public Repository(IUnitOfWork uow)
{
_uow = uow;
}
public Repository()
{
}
#region IRepository<T> Members
public IList<T> GetAll()
{
using (var session = _uow.CurrentSession)
{
return session.CreateCriteria(typeof (T)).List<T>();
}
}
public IList<T> FindAll<T>(IList<Expression<Func<T, bool>>> criteria)
{
var session = _uow.CurrentSession;
var query = from item in session.SessionFactory.OpenSession().Query<T>()
select item;
foreach (var criterion in criteria)
{
query = query.Where(criterion);
}
return query.ToList();
}
public T FindFirst<T>(IList<Expression<Func<T, bool>>> criteria)
{
var col = FindAll(criteria);
if (col.Count > 0)
{
return col.First();
}
else
{
return default(T);
}
}
public T Get(int id)
{
using (var session = _uow.CurrentSession)
{
return session.Get<T>(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
}
}
here is my bootstrapper:
public class BootStrapper : IBootstrapper
{
private static bool _hasStarted;
public virtual void BootstrapStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.Scan(s =>
{
s.TheCallingAssembly();
s.AssemblyContainingType<User>();
s.AssemblyContainingType<UserRepository>();
s.AssemblyContainingType<NHibernateRegistry>();
s.LookForRegistries();
});
// Repositories
x.For<WmcStar.Data.IUserRepository>()
.CacheBy(InstanceScope.HttpContext)
.TheDefault.Is.OfConcreteType<UserRepository>();
x.For<IDatabaseBuilder>().TheDefaultIsConcreteType<DatabaseBuilder>();
});
}
public static void Restart()
{
if (_hasStarted)
{
ObjectFactory.ResetDefaults();
}
else
{
Bootstrap();
_hasStarted = true;
}
}
public static void Bootstrap()
{
new BootStrapper().BootstrapStructureMap();
}
}
here is my NHibernateRegistry:
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<Configuration>().Singleton().Use(cfg);
For<ISessionFactory>().Singleton().Use(sessionFactory);
For<ISession>().HybridHttpOrThreadLocalScoped()
.Use(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());
For<IUnitOfWork>().HybridHttpOrThreadLocalScoped()
.Use<UnitOfWork>();
For<IDatabaseBuilder>().Use<DatabaseBuilder>();
SetAllProperties(x => x.OfType<IUnitOfWork>());
}
}
and finaly here is my global.asax:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
BootStrapper.Bootstrap();
new SchemaExport(ObjectFactory.GetInstance<Configuration>()).Execute(false, true, false);
ObjectFactory.GetInstance<IDatabaseBuilder>().RebuildDatabase();
AutoMapper.Mapper.CreateMap<WmcStar.Model.User, WmcStar.Data.Dto.User>();
}
}
Anyone got any clues as to what would cause this?
w://