I have the following code in an Autofac Module that is used in my Application_Start method:
builder.Register(c => new Configuration().Configure().BuildSessionFactory())
.SingletonScoped();
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession())
.HttpRequestScoped();
builder.Register<NHibernateSomethingRepository>().As<ISomethingRepository>();
The constructor for the repository takes an ISession as argument. But I end up with one session for the whole application, even though I explicitly asked for it to be HttpRequestScoped.
I have configured the ContainerDisposal HTTP module.
According to the documentation you have to create a nested container, but I'm letting Autofac autowire the dependencies.
What should I do?
Thanks!
I found the problem, so I will answer my own question.
I registered my repository with the default scope, which in Autofac is singleton scope. I should have done this:
builder.Register<NHibernateSomethingRepository>()
.As<ISomethingRepository>()
.HttpRequestScoped;
来源:https://stackoverflow.com/questions/359644/how-do-i-make-sure-that-there-is-one-nhibernate-isession-per-request-using-autof