可以将多个Autofac寿命范围可以在指定的注册? [英] Can multiple Autofac lifetime scopes be specified on a registration?

查看:249
本文介绍了可以将多个Autofac寿命范围可以在指定的注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是附加在其上提供InstancePerHtt prequest一生范围MVC4的Autofac IoC容器。但是我的项目中我有网络,网络API和后台工作线程。在下面的例子中,我假设InstancePerHtt prequest范围没有多大的意义时不是从Web请求发起。

I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the InstancePerHttpRequest scope doesn't mean much when not originating from a web request.

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

我不知道是否有可能做类似下面的例子,并有容器选择最适当的寿命范围是什么?

I'm wondering if it is possible to do something like the following example and have the container choose the most appropriate lifetime scope?

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

在这种情况下,我打算发生的是如果请求来自Web请求源自那么它会选择InstancePerHtt prequest范围,如果它来自的WebAPI请求时,它会选择InstancePerApiRequest范围起源,如果它是用来应用程序的工作线程将使用InstancePerD​​ependency范围是什么?

In this case what I intend to happen is if the request originates from a web request then it will choose the InstancePerHttpRequest scope, if it originates from an webApi request it will choose the InstancePerApiRequest scope and if it is used by the application worker threads it will use the InstancePerDependency scope?

任何想法,如果这或者类似的东西是可能的?结果
谢谢

Any ideas if this or something similar is possible?
Thanks

推荐答案

这问题有一些相当沉重的这些重叠的:

This question has some fairly heavy overlap with these:

  • Managing AutoFac lifetime scopes per session and request in asp.net mvc 3
  • Instance per matching lifetime scope, with default?
  • Autofac - Lifetime and modules
  • Autofac Lifetimes and the Default Provider within a Matching Lifetime Scope

您需要检查那些出一些想法。

You'll want to check those out for some ideas.

简短的回答是:。这种事不支持开箱即用您需要做两件事情之一

The short answer is: This sort of thing isn't supported out of the box. You'll need to do one of a couple of things.

选项:您可能对后台线程不同的容器这不会让你分享应用程序级的单身人士,但可能是您的应用程序确定

Option: You could have a different container for the background threads. This wouldn't allow you to share application-level singletons, but that might be OK for your application.

选项:您可以创建两个生命周期范围从容器和做不同的登记为 BeginLifetimeScope 调用的一部分这将使您共享应用程序级的单身人士和有在不同的上下文相同的组件不同的生命周期作用域。这是一个有点难以管理的注册,不过,你需要两个不同的服务定位器(例如, DependencyResolver ),因为每个逻辑方​​面都需要从自己的范围,以解决

Option: You could create two lifetime scopes off the container and do the different registrations as part of the call to BeginLifetimeScope. This would allow you to share application-level singletons and have different lifetime scopes for the same components in different contexts. It's a little harder to manage the registrations, though, and you'll need two different service locators (e.g., DependencyResolver) because each logical context would need to resolve from its own scope.

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

如果你真的想获得看上了这个选项,你可以实现自定义的的IContainer ,可以检测它的这上下文,并从相应的嵌套的作用域解决的事情。这是怎样的多租户支持Autofac作品。然而,这是一个更积极参与的解决方案,所以我不打算写,所有在这里。检查的例子多租户支持Autofac源。

If you really wanted to get fancy with this option, you could implement a custom IContainer that can detect which context it's in and resolves things from the appropriate nested scope. This is how the multitenant Autofac support works. However, that's a much more involved solution so I'm not going to write that all out here. Check the Autofac source for the multitenant support for examples.

选项:你可以使用一个最小公分母之类的登记 InstancePerD​​ependency InstancePerLifetimeScope 键,跳过对应用程序的不同部分的使用寿命是不同的概念。

Option: You could use a "lowest common denominator" sort of registration like InstancePerDependency or InstancePerLifetimeScope and skip the notion of having a different lifetime for different parts of the application.

需要注意的是,现在,在技术上,在内部,之间存在什么 InstancePerHtt prequest InstancePerWebApiRequest 。他们都归结为同样的事情,并有效地互换。 (我不能保证它永远是永远这样,但我不知道为什么它会需要改变。)

Note that, right now, technically, internally, there is no difference between what InstancePerHttpRequest and InstancePerWebApiRequest do. They both boil down to exactly the same thing and are effectively interchangeable. (I can't promise it'll always be that way forever, but I don't know why it would need to change.)

这篇关于可以将多个Autofac寿命范围可以在指定的注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆