How to get rid of NHibernate ISession in an ASP.NET MVC application

I have NHibernate connected in my asp.net mvc application.

Everything works fine if I DO NOT DELETE. However, I read that you should dispose, but when I do this, I get random exceptions "session closed".

I injected ISession into my other objects using Windsor.

Here is my current NHModule:

public class NHibernateHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
        context.EndRequest += context_EndRequest;
    }

    static void context_EndRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Unbind(MvcApplication.SessionFactory);
    }

    static void context_BeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(MvcApplication.SessionFactory.OpenSession());
    }

    public void Dispose()
    {
        // do nothing
    }
}

Register ISession:

container
  .Register(Component.For<ISession>()
     .UsingFactoryMethod(() =>  MvcApplication.SessionFactory.GetCurrentSession()).LifeStyle.Transient);

The error occurs when I bind Dispose to unbind in the module. As I continue to consider the session a closed error, I assume that this is not the right way to do this, so is this the right way?

Thanks Joe

+3
source share
3 answers

, Windsor , , . , HttpModule, Windsor PerWebRequest ISession.

+1

, - , , , . , NHiberanteSessionFactory Dispose , .

0

, : NHibernate

, , MSDN.

0

Source: https://habr.com/ru/post/1729816/


All Articles