views:

248

answers:

2

I previously asked a question here about autofac not disposing my objects when the HTTP request ends. I now think I have a bigger problem, becuasse there is evidence that it is serving up the SAME object request-to-request. Again, I am using thier instructions here. My test is a bit more complex because I'm using the delegate syntax to create an object but I think I'm flagging it for request-lifetime. Global.asax.cs:

protected void Application_Start(object sender, EventArgs e)
{
    ...
    var builder = new Autofac.Builder.ContainerBuilder();
    builder.Register<IDBConnectionSelector>(
            (c) => new CachingDBConnections(ConstructorArgs...))
        .HttpRequestScoped();

    var container = builder.Build();
    _containerProvider = new ContainerProvider(container);
}
public IContainerProvider ContainerProvider
{
    get { return _containerProvider; }
}
static IContainerProvider _containerProvider;

My intention here is to register IDBConnectionSelector to get the concrete type CachingDBConnections created with a custom constructor but with HTTP request scope.

Some methods of the CachingDBConnections object is failing on subsequent requests in a way that leads me to believe I'm getting the same one I got last time and not a NEW one for every request.

Does that make sense? What am I doing wrong?

+2  A: 

Most of the time this happens (in any IoC container) you'll find that one component along a chain of dependencies is a singleton.

E.g.

A -> B -> C

If A is 'factory', B is 'singleton' and C is 'factory', then resolving A will get a reference to the singleton B, which will always reference the same C.

In order for a new C to get created every time you resolve A, B must also be 'factory'.

How's my guess? Is this the problem?

Nick

Hi Nicholas! Thanks so much for checking in. I figured it out and posted my answer...
n8wrl
+1  A: 

I figured it out!

I was asking the WRONG container for the object instance - I was asking the application-container for the object and not the request-container.

D'oh!

n8wrl