This question has a rather difficult coincidence with them:
You want to test these ideas for some ideas.
Short answer: this kind of thing is not supported out of the box. You will need to do one of two things.
Option: you may have another container for background threads. This will not allow you to share single-level application tiers, but it might be ok for your application.
Option: you can create two containers with a lifespan from the container and perform various registrations as part of the BeginLifetimeScope call. . This will allow you to exchange peer-tier application tiers and have different lifespan for the same components in different contexts. However, itβs a little more difficult to manage registrations, and you will need two different service locators (for example, DependencyResolver ), because each logical context would have to be resolved from its own area.
var builder = new ContainerBuilder(); builder.RegisterType<AppLevelSingleton>().SingleInstance(); var container = builder.Build();
If you really want to get an idea about this parameter, you can implement a custom IContainer that can determine in what context it is located and allows things from the corresponding nested area. This is how Autofac multi-user support works. However, this is a much more complicated solution, so I'm not going to write everything here. For an example, check the Autofac source for multican support.
Option: you can use the registration type "lowest common denominator" , for example InstancePerDependency or InstancePerLifetimeScope , and skip the notion that there is a different lifetime for different parts of the application.
Please note that now, technically, internally, there is no difference between what InstancePerHttpRequest and InstancePerWebApiRequest do . They both boil down to the same thing and are effectively interchangeable. (I cannot promise that this will always be forever, but I do not know why this should change.)
Travis illig
source share