我在风中飘扬,所以我想我会在这里问...如果这很明显并且以前已经回答过,请告诉我。
我正在构建一个 MVC 3 站点,当我与一个用户一起运行它时,它运行良好,我在其中单击页面。但是,如果我疯狂地点击刷新,最终我会点击“会话已关闭”。
我已经隔离了几乎所有的存储库以试图深入了解,所以我知道它在主页上出错了。存储库中唯一调用的是从数据库表中获取用户名。
我使用 Postgres 作为数据库,以及来自 NauckIT 的 ASP.NET Membership 提供程序。主数据库也是 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只需userid从表单身份验证 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 时,我遇到了会话关闭问题。
更新:不确定它是否相关,但我在BaseController, 从OnActionExecuting方法中看到的主要位置。在上面的方法中似乎已经清除了。