Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac Scoping Of Unit Of Work In MVC3 With Entity Framework

We have a standard "chain" of modules that MVC3 controller creates and calls (directly or indirectly) - "MyController" -> "MyService" -> "MyRepository" -> "MyUnitOfWork"

The modules are registered in App_Start as following:

var builder = new ContainerBuilder();

builder.RegisterType<MyRepository>().AsImplementedInterfaces().InstancePerHttpRequest();
builder.RegisterType<MyUnitOfWork>().AsSelf().InstancePerHttpRequest();
builder.RegisterType<MyService>().AsImplementedInterfaces().InstancePerHttpRequest();

IContainer container = builder.Build();
DependencyResolver.SetResolver( new AutofacDependencyResolver( container ) );

When we try to perform multiple calls to the service from the controller in parallel, we're getting exceptions in EF4 (we use DbContext as a base class for UoW).

What would be a correct scoping of the modules to make parallel calls to service in the context of the same HttpRequest?

Thank you :)

like image 448
ZENIT Avatar asked Nov 27 '25 14:11

ZENIT


1 Answers

Sounds like 1 DbContext per HttpRequest isn't gonna work for you. You probably need InstancePerDependency() and then request a new DbContext per thread.

like image 166
Jim Bolla Avatar answered Nov 29 '25 04:11

Jim Bolla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!