Can I specify multiple areas of the Autofac life cycle during registration?

I am using an Autofac IoC container with an MVC4 add-in that provides the scope of an InstancePerHttpRequest instance. However, as part of my project, I have web pages, web-api, and background workflows. In the following example, I assume that the InstancePerHttpRequest scope does not matter much if it does not arise from a web request.

builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() 

I am wondering if it is possible to do something like the following example and select a container of the most suitable scale?

 builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() .InstancePerApiRequest() .InstancePerDependency(); 

In this case, what I intend to accomplish is if the request arises from the web request, then it will select the InstancePerHttpRequest region, if it arose from the webApi request, it will select the InstancePerApiRequest region and if it will be used by the thread application worker it will use the region InstancePerDependency?

Any ideas if this or something similar is possible?
Thanks

+7
source share
1 answer

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(); // Create a nested lifetime scope for your background threads // that registers things as InstancePerDependency, etc. Pass // that scope to whatever handles dependency resolution on the thread. var backgroundScope = container.BeginLifetimeScope( b => b.RegisterType<DatabaseFactory>() .As<IDatabaseFactory>() .InstancePerDependency()); // Create a nested lifetime scope for the web app that registers // things as InstancePerHttpRequest, etc. Pass that scope // as the basis for the MVC dependency resolver. var webScope = container.BeginLifetimeScope( b => b.RegisterType<DatabaseFactory>() .As<IDatabaseFactory>() .InstancePerHttpRequest()); var resolver = new AutofacDependencyResolver(webScope); DependencyResolver.SetResolver(resolver); 

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.)

+10
source

All Articles