Question

I have a Mvc system working fine with NHibernate and Autofac... I´m doing that to manage the NH sessions :

public static void Init(Assembly assembly)
{            
     var builder = new ContainerBuilder();

     builder.RegisterControllers(assembly);

     builder.Register(x => NHibernateInitializer.BuildSessionFactory()).SingleInstance();
     builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).InstancePerHttpRequest();

     builder.RegisterModule(new AutofacWebTypesModule());
     var container = builder.Build();
     DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

}

Now I need to configure a Windows Service to use NHibernate, but I´m not sure if It is possible with Autofac. Any idea?

Thanks

Was it helpful?

Solution

I would register ISession and other services as .InstancePerLifetimeScope() and then have each operation be run in its own child LifetimeScope. This would ensure that all the IDisposable services get cleaned up nicely at the end of the operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top