0

I have WebApi project that has reference on other my project DataAccess.

In DataAccess I have registered IDbSessionManager as

builder.RegisterType<DbSessionManager>().As<IDbSessionManager>();

In WebApi project I want to override this registration in next way:

builder.RegisterType<DbSessionManager>().As<IDbSessionManager>().InstancePerRequest();

If I add both registrations, then Autofac throws exception

No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

It can be solved if I remove registration from WebApi project or change it in this way:

builder.RegisterType<DbSessionManager>().As<IDbSessionManager>().SingleInstance();

But I need that IDbSessionManager was resolved per request.

Previously there was one solution to update container but now Update method are deprecated.

Is there any other way of doing this? Maybe using extension method like OnlyIf ?

stuartd
  • 70,509
  • 14
  • 132
  • 163
demo
  • 6,038
  • 19
  • 75
  • 149
  • This error means that you are resolving something outside a http context request. Is it normal that something in your application try to resolve such a type without current http context? Could you describe your desired *Autofac* behavior in such a case ? – Cyril Durand Jan 15 '18 at 15:50
  • Problem - I can't find that something... – demo Jan 15 '18 at 16:23
  • You can have a look at the call stack. It usually contains lot of information to find this *something*. This link from the documentation will also helps you : http://autofac.readthedocs.io/en/latest/faq/per-request-scope.html#troubleshooting-per-request-dependencies – Cyril Durand Jan 15 '18 at 16:26
  • See [this answer](https://stackoverflow.com/a/9503612). Whether `DataAccess` is a layer of an application or a [framework](http://blog.ploeh.dk/2014/05/19/di-friendly-framework/), it should not have any Autofac DI configuration within it. If you put all of your composition root in your *entry assembly*, there will be no need to override any existing configuration. Related: [composition root reuse](http://blog.ploeh.dk/2015/01/06/composition-root-reuse/). – NightOwl888 Jan 16 '18 at 02:37

0 Answers0