c# - NHibernate 抛出 Session 已关闭

标签 c# asp.net-mvc-3 nhibernate fluent-nhibernate ninject

我在风中飘荡,所以我想我应该在这里问...如果这很明显并且之前已经回答过,请告诉我。

我正在构建一个 MVC 3 站点,当我与一个用户一起运行它时,它运行良好,我在其中单击浏览页面。但是,如果我疯狂地点击刷新,最终我会点击“ session 已关闭”。

我已经隔离了几乎所有我的存储库以试图找到底部,所以我知道它在主页上有错误。存储库中唯一被调用的是从数据库表中获取用户名。

我将 Postgres 用作数据库,并使用 NauckIT 的 ASP.NET 成员资格提供程序。主数据库也是 Postgres(但另一个数据库)。

session 管理使用以下代码完成:

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 中获取 userid

使用以下方法将 session 注入(inject)到存储库中:

        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 时,我遇到了 session 关闭的问题。

更新: 不确定它是否相关,但我在 BaseController 中看到它的主要位置,来自 OnActionExecuting 方法。上面的方法好像都搞清楚了。

最佳答案

您不应在 Web 应用程序中使用 InThreadScope()。使用 InRequestScope()。编辑阅读 Object Scopes - 它是最近更新的,不知道它的倒退迟早会浪费您的时间!

如果您希望在成员(member)提供程序和请求处理中实现某些功能,您需要搜索 Ninject Custom Provider(可能类似于 here)。

关于c# - NHibernate 抛出 Session 已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16657224/

相关文章:

asp.net - 在 MVC3 编辑器模板中获取验证属性

c# - MVC 3(预览版 1)动态 View 模型

c# - Fluent Nhibernate继承抛出索引超出范围异常

.net - 使用 Ignore.NotFound 延迟加载 NHibernate

c# - 使用 IEnumerator 遍历列表

c# - 解决方案 - 实体类型 ApplicationUser 不是当前上下文模型的一部分

Ajax.ActionLink 没有 href 属性以获得更好的 SEO

c# - NHibernate 是否比经典的 ODBC 驱动程序更快?

c# - 构建表达式以使用 DateTime 调用 StringBuilder.Append(Object) 时出现异常

c# - 如何在 <button/> 上使用 mouseDown 和 mouseUp