new NHibe-6ren">
gpt4 book ai didi

asp.net-mvc - Autofac,(流利)nHibernate,ISession "Session is closed!"间歇性

转载 作者:行者123 更新时间:2023-12-04 20:06:21 26 4
gpt4 key购买 nike

我有随机“ session 已关闭!” Autofac 和 Fluent nHibernate 的以下配置错误:

全局.asax.cs:

builder.Register(x => new NHibernateConfigurator().GetSessionFactory()).SingleInstance();
builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).InstancePerHttpRequest();

NHibernateConfigurator.cs
    public class NHibernateConfigurator
{
public Configuration Configure()
{
var configuration = new Configuration();
configuration.SessionFactory()
.Proxy.Through<ProxyFactoryFactory>()
.Integrate.Using<Oracle10gDialect>();

FluentConfiguration fluentConfiguration = Fluently.Configure(configuration);
fluentConfiguration.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>());

return fluentConfiguration.BuildConfiguration();
}

public ISessionFactory GetSessionFactory()
{
var configuration = Configure();
return configuration.BuildSessionFactory();
}
}

SomeController.cs:
private readonly IRepository repository;

public SomeController(IRepository repository)
{
this.repository = repository
}

[Transaction]
public ActionResult Index()
{
var result = repository.GetUsers();
return View(result);
}

事务属性.cs
public class TransactionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
DependencyResolver.Current.GetService<ISession>().BeginTransaction(IsolationLevel.ReadCommitted);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ITransaction currentTransaction = DependencyResolver.Current.GetService<ISession>().Transaction;

if (currentTransaction.IsActive)
{
if (filterContext.Exception != null && filterContext.ExceptionHandled)
{
currentTransaction.Rollback();
}
}
}

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
ITransaction currentTransaction = DependencyResolver.Current.GetService<ISession>().Transaction;

base.OnResultExecuted(filterContext);
try
{
if (currentTransaction.IsActive)
{
if (filterContext.Exception != null && !filterContext.ExceptionHandled)
{
currentTransaction.Rollback();
}
else
{
currentTransaction.Commit();
}
}
}
finally
{
currentTransaction.Dispose();
}
}
}

IRepository.cs:
public interface IRepository 
{
IList<User> GetUsers();
}

存储库.cs:
public class Repository : IRepository
{
private readonly ISession session;

public Repository(ISession session)
{
this.session = session;
}

public IList<User> GetUsers()
{
return session.QueryOver<User>().List();
}
}

当前的设置有效,但似乎间歇性失败(在重新加载几页页面或重新启动 Cassini 后),GetUsers 抛出“ session 已关闭!”错误。我认为在 InstancePerHttpRequest 上注册 ISessionFactory 可以避免这些问题。没运气。有任何想法吗? nHibernate 和 Autofac 的新手,所以如果我没有发布足够的相关信息,请告诉我。

最佳答案

看来我的配置对于我的自定义成员资格提供程序和自定义角色提供程序之外的任何 nHibernate 调用都非常有效 - 据我所知,它们是问题,这意味着这个问题并没有真正解决问题。

关于asp.net-mvc - Autofac,(流利)nHibernate,ISession "Session is closed!"间歇性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com