注意:现在我已经打印出来了,我不得不为这个超长的问题道歉,但是,我认为这里提供的所有代码和信息在某种程度上是相关的。
好吧,在我的ASP.NETWebForms应用程序中随机出现“会话已关闭”错误。然而,今天,它终于一次又一次地发生在同一个地方。我几乎可以肯定的是,在我的代码中没有任何东西在处理或关闭会话,因为使用的代码位被很好地包含在所有其他代码之外,您将在下面看到。
好吧,那么,我的第一个
SessionFactoryProvider
和
SessionProvider
课程:
public class SessionFactoryProvider : IDisposable
{
ISessionFactory sessionFactory;
public ISessionFactory GetSessionFactory()
{
if (sessionFactory == null)
sessionFactory =
Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2005.ConnectionString(p =>
p.FromConnectionStringWithKey("QoiSqlConnection")))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<JobMapping>())
.BuildSessionFactory();
return sessionFactory;
}
public void Dispose()
{
if (sessionFactory != null)
sessionFactory.Dispose();
}
}
会话提供程序
public class SessionProvider : IDisposable
{
ISessionFactory sessionFactory;
ISession session;
public SessionProvider(SessionFactoryProvider sessionFactoryProvider)
{
this.sessionFactory = sessionFactoryProvider.GetSessionFactory();
}
public ISession GetCurrentSession()
{
if (session == null)
session = sessionFactory.OpenSession();
return session;
}
public void Dispose()
{
if (session != null)
{
session.Dispose();
}
}
}
这两个类与Ninject连接起来,如下所示:
NHibernateModule公司
public class NHibernateModule : StandardModule
{
public override void Load()
{
Bind<SessionFactoryProvider>().ToSelf().Using<SingletonBehavior>();
Bind<SessionProvider>().ToSelf().Using<OnePerRequestBehavior>();
}
}
据我所知,工作和预期一样。
BaseDao<T>
班级:
BaseDao公司
public class BaseDao<T> : IDao<T> where T : EntityBase
{
private SessionProvider sessionManager;
protected ISession session { get { return sessionManager.GetCurrentSession(); } }
public BaseDao(SessionProvider sessionManager)
{
this.sessionManager = sessionManager;
}
public T GetBy(int id)
{
return session.Get<T>(id);
}
public void Save(T item)
{
using (var transaction = session.BeginTransaction())
{
session.SaveOrUpdate(item);
transaction.Commit();
}
}
public void Delete(T item)
{
using (var transaction = session.BeginTransaction())
{
session.Delete(item);
transaction.Commit();
}
}
public IList<T> GetAll()
{
return session.CreateCriteria<T>().List<T>();
}
public IQueryable<T> Query()
{
return session.Linq<T>();
}
}
它是这样装订在一起的:
道模
public class DaoModule : StandardModule
{
public override void Load()
{
Bind(typeof(IDao<>)).To(typeof(BaseDao<>))
.Using<OnePerRequestBehavior>();
}
}
导致这种情况的web请求是在我保存对象时发生的,直到今天我对模型做了一些更改之后才发生,但是对模型的更改并没有改变数据访问代码。虽然它改变了一些NHibernate映射(如果有人感兴趣,我也可以发布这些)
据我所知,
BaseDao<SomeClass>.Get
那就叫什么
BaseDao<SomeOtherClass>.Get
BaseDao<TypeImTryingToSave>.Save
被称为。
这是我接到的第三个电话
Save()
using (var transaction = session.BeginTransaction())
以“会话已关闭!”或者说是例外:
Session is closed!
Object name: 'ISession'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
实际上,在调试器上的后续操作显示了第三次从调试器请求会话
它确实是封闭的,没有连接。
我已经证实了
Dispose
在我的
在我的
会话提供程序
Save
召唤我的刀。
所以现在我有点困了。我突然想到了几件事。
提前谢谢