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.
NHibernate session handler with Autofac in a Windows service
-
03-07-2023 - |
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow