简体   繁体   English

如何使用Nhibernate在ASP.NET MVC中实现按请求的会话模式

[英]How to implement session-per-request pattern in asp.net mvc with Nhibernate

I created the nhibernate session in Application_start event of global.asax file,the session is being passed to constructors of service methods. 我在global.asax文件的Application_start事件中创建了nhibernate会话,该会话正在传递给服务方法的构造函数。

In the service method I am using the session to do CRUD operations, this works fine.However, When multiple requests or parallel transactions occuring nhibernate is throwing some exceptions.After reading forums i came to know that Nhibernate session is not thread safe.How to make it thread safe and let my application (ASP.NET mvc) work with parallel trandsactions? 在服务方法中,我使用会话执行CRUD操作,这很好用,但是,当发生多个请求或并行事务时,nhibernate抛出了一些异常。在阅读了论坛后,我知道Nhibernate会话不是线程安全的。使它线程安全,并让我的应用程序(ASP.NET MVC)与并行事务一起工作?

Only way to make it thread safe is to create a new session per each request, you could use current_session_context_class property to managed_web in NHibernate config. 使线程安全的唯一方法是为每个请求创建一个新会话,您可以在NHibernate配置中将current_session_context_class属性用于managed_web

In global.asax 在global.asax中

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var session = SessionFactory.OpenSession();
        CurrentSessionContext.Bind(session);
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var session = CurrentSessionContext.Unbind(SessionFactory);
        //commit transaction and close the session
    }

now when you want to access the session, you could use, 现在,当您要访问会话时,可以使用

Global.SessionFactory.GetCurrentSession()

If you are using a DI container, it's usually built into the container, 如果您使用的是DI容器,则通常是内置在该容器中的,

For example for Autofac (see this question for more information), 以Autofac为例(有关更多信息,请参见此问题 ),

containerBuilder.Register(x => {
    return x.Resolve<ISessionFactory>().OpenSession(); 
}).As<ISession>().InstancePerHttpRequest();

Store it in the HttpContext. 将其存储在HttpContext中。

Add this to your global.asax 将此添加到您的global.asax

    public static String sessionkey = "current.session";

    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();
    }

And here is the component registration 这是组件注册

public class SessionInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container
            .Register(Component.For<ISession>().UsingFactoryMethod(() => MvcApplication.CurrentSession)
            .LifeStyle
            .PerWebRequest);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个请求会话MVC应用程序的NHibernate问题 - NHibernate problems with a session-per-request MVC appli 对于每个请求的会话,应如何在Nancy中处理NHibernate会话? - How should NHibernate session be handled in Nancy for session-per-request? nHibernate每个请求的会话会话结束前删除并提交 - nHibernate Session-Per-Request Delete and commit before session ends 从控制器获取Nhibernate ISession(每个请求会话)ASP.Net MVC - Getting Nhibernate ISession from Controller (Session Per Request) ASP.Net MVC ASP.NET C#+ Nhibernate HTTP模块(每个请求会话)-限制请求类型 - ASP.NET C# + Nhibernate HTTP module (Session per request) - restrict request types 如何使用WebAPI / NHibernate实现每次会话会话模式 - How to implement Session Per Conversation pattern with WebAPI/ NHibernate ASP.NET MVC按请求注入 - ASP.NET MVC inject per request 在asp.net mvc 3中管理每个会话的AutoFac生命周期范围和请求 - Managing AutoFac lifetime scopes per session and request in asp.net mvc 3 架构问题:Fluent NHibernate,Repository模式和ASP.NET MVC - Architectural concerns: Fluent NHibernate, The Repository pattern and ASP.NET MVC ASP.net 4.6 MVC 5每个会话的Autofac对象 - ASP.net 4.6 MVC 5 Autofac Object per Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM