Fluent nhibernate 如何加快我的Nhibernate配置

Fluent nhibernate 如何加快我的Nhibernate配置,fluent-nhibernate,asp.net-mvc-4,Fluent Nhibernate,Asp.net Mvc 4,我有一个ASP.NET MVC4 web api,它使用fluent NHibernate,但是当我运行调试时,第一次尝试访问任何使用数据库的站点需要1½分钟,因此我发现,但我不确定如何利用它 当它在实时服务器上运行时,它必须对会话数据进行垃圾收集,因为如果我很长时间没有使用它,重新配置它需要很长时间 有没有可能把时间从1.5分钟缩短到10秒以下 我当前的SessionFactory类如下所示 public class SessionFactory { private static re

我有一个ASP.NET MVC4 web api,它使用fluent NHibernate,但是当我运行调试时,第一次尝试访问任何使用数据库的站点需要1½分钟,因此我发现,但我不确定如何利用它

当它在实时服务器上运行时,它必须对会话数据进行垃圾收集,因为如果我很长时间没有使用它,重新配置它需要很长时间

有没有可能把时间从1.5分钟缩短到10秒以下

我当前的SessionFactory类如下所示

public class SessionFactory
{
    private static readonly string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;

    private static ISessionFactory _session;
    private static readonly object SyncRoot = new Object();

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(MySQLConfiguration
            .Standard
            .ConnectionString(ConnString))
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())
                .ExposeConfiguration(UpdateSchema)
                .BuildSessionFactory();
    }

    private static void UpdateSchema(Configuration cfg)
    {
        new SchemaUpdate(cfg).Execute(false, true);
    }

    public static ISession Session
    {
        get
        {
            if (_session == null)
            {
                lock (SyncRoot)
                {
                    if (_session == null)
                        _session = CreateSessionFactory();
                }
            }
            return _session.OpenSession();
        }
    }
}
公共类SessionFactory
{
私有静态只读字符串ConnString=System.Configuration.ConfigurationManager.ConnectionString[“MySQLConnectionString”].ConnectionString;
私有静态ISessionFactory会话;
私有静态只读对象SyncRoot=新对象();
私有静态ISessionFactory CreateSessionFactory()
{
流畅地返回。Configure()
.数据库(MySQLConfiguration
标准
.ConnectionString(连接字符串))
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf())
.ExposeConfiguration(UpdateSchema)
.BuildSessionFactory();
}
私有静态void UpdateSchema(配置cfg)
{
newschemaupdate(cfg).Execute(false,true);
}
公众休会期
{
得到
{
如果(_session==null)
{
锁定(同步根)
{
如果(_session==null)
_会话=CreateSessionFactory();
}
}
返回_session.OpenSession();
}
}
}
这是一个点跟踪的屏幕截图


在我的主机上为请求计时初始请求需要1分50秒,这是在“实时”服务器上(我无法控制那里的IIS)

我可以看到此代码有很多错误。您的CreateSessionFactory在应用程序的生命周期中只能调用一次。在global.asax中创建一个静态变量,并在应用程序_Start()中调用CreateSessionFactory一次。同样在global.asax中,您希望为应用程序_BeginRequest和应用程序_EndRequest设置一些事件。这就是您要创建和破坏会话的地方。这是您在web应用程序中的工作单元。您还应该将会话存储在HttpContext中

public static ISession CurrentSession
{
    get { return (ISession)HttpContext.Current.Items[sessionkey]; }
    set { HttpContext.Current.Items[sessionkey] = value; }
}

protected void Application_BeginRequest()
{
    CurrentSession = SessionFactory.OpenSession();
}

protected void Application_EndRequest()
{
    if (CurrentSession != null)
        CurrentSession.Dispose();
}

每次需要会话时,您的代码都会重新创建SessionFactory。

您是否在不使用UpdateSchema的情况下尝试过它?不要认为每次模型更改只需要执行一次以上的操作。它应该是什么样子的,我想我应该为普通请求更新一次模式,然后在需要创建任何内容时导出模式?您可以只注释掉。ExposeConfiguration(UpdateSchema)看看现在是否更快。。。最好在QA服务器上。=)我想是这样的,现在在调试模式下,时间缩短到不到10秒,你能给我一个答复吗?我可以标记为答案:)请注意:如果您再次查看他的代码,您将看到Mech0z正在为工厂使用a(误导性地称为“_session”)。工厂仅在第一次请求会话时创建。噢。我现在明白了。我的坏消息。@Fran好消息!这确实帮助我理解了如何在ASP.NET web api中使用FNH进行设计。我还遇到了这样一种情况:当调试应用程序启动时,调试器不会连接,如果其他人看到该问题,请查看该线程-