开发者

Correct way of bootstrapping NHibernate in MVC

开发者 https://www.devze.com 2023-01-15 16:37 出处:网络
I need to setup session management in MVC. what is the correct way of doing so? How to setup nhibernate session management in mvc using structuremap so I don\'t get:

I need to setup session management in MVC. what is the correct way of doing so? How to setup nhibernate session management in mvc using structuremap so I don't get:

Session is closed or Using a single Session in multiple threads 开发者_如何学Cis likely a bug.

My current configuration is: in GlobalAssax:

protected void Application_Start()
    {
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
        Bootstrapper.ConfigureStructureMap();
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);

    }

in my BootStrapper I do:

var cfg = NHibernateManager.Configuration(assembly);
For<Configuration>().Singleton().Use(cfg);
For<ISessionFactory>().Singleton().Use(cfg.BuildSessionFactory());
For<ISession>().HttpContextScoped().Use(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());

I Inject ISession into repositoryes that I use in application layer.

Edit: What happens if I do this?: For().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use(ctx => ctx.GetInstance().OpenSession());


Have you added a dispose for the session?

//In Global.asax.cs
protected void Application_EndRequest()
{
    ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}

Otherwise it looks correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号