我在风中拍打,所以我想我会在这里问…如果这是显而易见的,请让我知道,之前已经回答过.
我正在构建一个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时,我得到会话关闭问题.
更新:
不确定它是否相关,但是我在OnActionExecuting方法中在BaseController中看到的主要位置.它似乎已经在上述方法中得到了澄清.
解决方法
您不应该在Web应用程序中使用InThreadScope().使用InRequestScope().编辑阅读
Object Scopes – 最近更新,不知道它倒退会迟早浪费你的时间!
如果您希望通过成员资格提供商处理工作并请求处理,则需要搜索Ninject Custom Provider(可能类似于here).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。