Frage

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

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top