I am using AutoFac as a IoC to inject the ObjectContext inside the Controllers that needs it.
The ObjectContext is registered in AutoFac using InstancePerLifetimeScope, and I have created a custom Controller factory that resolves the correct controller from the DI-container, and thereby gets the ObjectContext injected.
When I request a page, everything looks fine, and the data is fetched from the database using EF4 as expected.
The behavious I am wondering about is that after I have started up the web application, and I go to delete or add some entries from the database manually, those changes are not reflected inside my web app. I had 10 records in the database, deleted 5, but still all 10 records are fetched by EF. I thought that when I registered the ObjectContext in AutoFac with InstancePerLifetimeScope that meant a new ObjectContext got created per web request.
Now it seems that the same ObjectContext is used at all times, if I have not misunderstood something about how EF caches.
In addition to not seeing the changes I make to the database from other applications, I guess this will cause problems in production when all users that connect to the site uses the same ObjectContext, since the ObjectContext is not threadsafe.
Anyone knows where I went wrong here?
I would guess that your custom
IControllerFactoryis at fault. If you’re using the latest Autofac/MVC integration, useAutofacDependencyResolverrather than a controller factory.If using an older Autofac/MVC, use the provided controller factory and make sure the
ContainerDisposalModuledescribed on the Autofac wiki is correctly set up inWeb.config.