c# – NHibernate投掷会话已关闭
我在风中拍打,所以我想我会在这里问…如果这是显而易见的,请让我知道,之前已经回答过.
我正在构建一个MVC 3站点,当我与一个用户一起运行时,我点击了这些页面.但是,如果我疯狂地点击刷新,最终我点击了“会话已关闭”. 我几乎把所有的存储库都隔离出去试图找到底部,所以我知道在主页上有错误.在存储库中调用的唯一内容是从数据库表中获取用户的名称. 我正在使用Postgres作为数据库,以及来自NauckIT的ASP.NET成员资格提供程序.主数据库也是Postgres(但另一个数据库). 会话管理使用以下代码完成: public class MvcApplication : System.Web.HttpApplication { public static ISessionFactory SessionFactory = NHibernateHelper.GetSessionFactory(); public MvcApplication() { this.BeginRequest += MvcApplication_BeginRequest; this.EndRequest += MvcApplication_EndRequest; } void MvcApplication_BeginRequest(object sender,EventArgs e) { CurrentSessionContext.Bind(SessionFactory.OpenSession()); } void MvcApplication_EndRequest(object sender,EventArgs e) { CurrentSessionContext.Unbind(SessionFactory).Dispose(); } } 获取登录信息的代码是: public Login GetCurrentLogin() { return Session.Query<Login>().FirstOrDefault(l => l.UserID == UserAccessRepository.UserID); } UserAccessRepository只是从表单身份验证cookie中获取用户标识. 会话使用以下内容注入存储库: ninjectKernel.Bind<IUserRepository>().To<NHUserRepository>(); ninjectKernel.Bind<ILeagueRepository>().To<NHLeagueRepository>().InThreadScope(); ninjectKernel.Bind<ISession>() .ToMethod(m => MvcApplication.SessionFactory.GetCurrentSession()) sessionfactory来自: public class NHibernateHelper { private static ISessionFactory _sessionFactory; public static ISessionFactory SessionFactory { get { if (_sessionFactory == null) { var rawConfig = new Configuration(); rawConfig.SetNamingStrategy(new PostgresNamingStrategy()); var configuration = Fluently.Configure(rawConfig) .Database(PostgreSQLConfiguration.Standard.ConnectionString(ConnectionString).ShowSql().Dialect("NHibernate.Dialect.PostgreSQL82Dialect")) .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Event>(new AutoMapConfiguration()) )).ExposeConfiguration(cfg => cfg.SetProperty("current_session_context_class","web") _sessionFactory = configuration.BuildSessionFactory(); Debug.WriteLine("Built SessionFactory"); } return _sessionFactory; 要清楚,它在我点击页面的标准实例中工作正常,但是当我疯狂地点击F5时,我得到会话关闭问题. 更新: 解决方法
您不应该在Web应用程序中使用InThreadScope().使用InRequestScope().编辑阅读
Object Scopes – 最近更新,不知道它倒退会迟早浪费你的时间!
如果您希望通过成员资格提供商处理工作并请求处理,则需要搜索Ninject Custom Provider(可能类似于here). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |