views:

645

answers:

1

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!

+6  A: 

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;
michielvoo
NOTE: In Autofac2 the default has changed - it is now: 'factoryScoped' AKA 'InstancePerDependancy' in the new parlance.
UpTheCreek