Reputation: 91
I have an MVC application with the following libraries installed.
I had a bug where the session is not getting closed after the request is done. I am having multiple sessions in the SQL database, however none of them are getting closed.
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder) {
var config = SessionFactory.ConofigCoreDb();
var factory = SessionFactory.BuildCloudSQLSessionFactory(config);
builder.RegisterInstance(config).As<Configuration>().SingleInstance();
builder.RegisterInstance(factory).As<ISessionFactory>().SingleInstance();
var session = builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerLifetimeScope();
//sql services
builder.RegisterType<ConsumerManager>().As<IConsumerManager>();
builder.RegisterType<DefaultLogger>().As<ILogger>();
builder.RegisterType<SettingsService>().As<ISettingService>();
// and so on...
}
The problem that am having is, with each call to any of the services the sql session is being generated and I can call the database however it is not getting disposed properlym, although the BaseRepository class has a disposable method which flushs the sessions and properly closes them.
How to get autofac to properly close the session safely?
Upvotes: 1
Views: 159
Reputation: 19630
Assuming you are using Autofac.Mvc integration library, you should register your session with InstancePerRequest. Currently you are registering PerLifetimeScope, which is not correct.
Upvotes: 4