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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top