Autofac - InstancePerHttpRequest 与 InstancePerLifetimeScope [英] Autofac - InstancePerHttpRequest vs InstancePerLifetimeScope

查看:61
本文介绍了Autofac - InstancePerHttpRequest 与 InstancePerLifetimeScope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个范围之间有什么区别?

What are the differences between the two scopes?

我正在每一层(存储库、服务、MVC 应用程序)中构建 Module(s),但为了拥有 InstancePerHttpRequest,您需要 Autofac.Mvc 程序集.

I am building Module(s) in each layer (Repository, Service, MVC App), but in order to have InstancePerHttpRequest you need the Autofac.Mvc assembly.

我应该在存储库和服务层中使用哪个范围?

Which scope should I be using in my Repository and Service layer?

推荐答案

InstancePerHttpRequestInstancePerApiRequest 本质上做同样的事情——你为每个服务获得一个单一的实例离散的网络请求.我将在答案的其余部分使用 InstancePerHttpRequest,但请记住,这两个是可以互换的.

InstancePerHttpRequest and InstancePerApiRequest essentially do the same thing - you get a single instance of your service for each discrete web request. I'll use InstancePerHttpRequest for the rest of the answer, but keep in mind that these two are interchangeable.

InstancePerLifetimeScope 表示将为请求您的服务的每个生命周期范围创建一个新的服务实例.每个 Web 请求都有自己的新生命周期范围,因此在实践中,这两个请求通常会做完全相同的事情.

InstancePerLifetimeScope means a new instance of the service will be created for every lifetime scope which asks for your service. Each web request gets its own fresh lifetime scope, so in practice, more often than not, these two will do the exact same thing.

唯一真正的区别在于,如果您在 InstancePerHttpRequest 下注册了一个服务,并且您从另一个注册为 SingleInstance 的服务中请求其中一个服务.在这种情况下:

The only real difference comes if you have a service registered under InstancePerHttpRequest and you request one of those services from another service which is registered as a SingleInstance. In this scenario:

  • SingleInstance 组件位于 root 范围
  • InstancePerHttpRequest 组件位于名为AutofacWebRequest"的作用域中,该作用域是根作用域的
  • The SingleInstance component lives in the root scope
  • The InstancePerHttpRequest component lives in a scope called "AutofacWebRequest", which is a child of the root scope

Autofac 不允许从子作用域解析 - 所以基本上,SingleInstance 服务找不到 InstancePerHttpRequest 服务.

Autofac does not allow for resolution from child scopes - so essentially, the SingleInstance service cannot find the InstancePerHttpRequest service.

但是,如果在这种情况下您使用了 InstancePerLifetimeScope(而不是 InstancePerHttpRequest),那么您的服务会很好地解析.

However, if in this scenario you had used InstancePerLifetimeScope (instead of InstancePerHttpRequest), then your services would resolve just fine.

我用可下载的代码写了一篇相当详尽的文章,试图详细解释所有这些 - 见这里.引用文章:

I've written up a fairly exhaustive article with downloadable code that attempts to explain all this in detail - see here. Quoting from the article:

这里的一个常见误解是,在 WebAPI 应用程序中使用 InstancePerLifetimeScope 注册您的组件意味着您的组件位于 Web 请求的范围内——即Lifetime"指的是Web 请求的生命周期".正如你在这里看到的,这是错误的.

One common misconception here is that registering your component with InstancePerLifetimeScope in a WebAPI application means that your component lives in the scope of a web request – i.e. that "Lifetime" refers to "the Lifetime of the web request". As you can see here, this is false.

您的组件的生命周期取决于它的解析范围.

由于 SingletonResolvable 从根范围解析其令牌,该令牌实例位于根范围内,而不是 Web 请求的范围内.我之前说过,但我再说一遍:这个令牌将一直存在,直到整个应用程序被处理掉(例如,IIS 工作进程被回收).任何从根作用域请求 ScopeToken 的东西都将被赋予对该令牌的引用.

Since the SingletonResolvable resolves its token from the root scope, that token instance lives in the root scope, not the scope of a web request. I’ve said it before, but I’ll say it again: this token will live until the entire application is disposed of (e.g. the IIS worker process is recycled). Anything which asks for a ScopeToken from the root scope will be given a reference to that token.

希望有所帮助 - 我很欣赏这个问题现在已经很老了,但它仍然非常相关!

Hope that helps - I appreciate this question is now quite old, however its still very relevant!

这篇关于Autofac - InstancePerHttpRequest 与 InstancePerLifetimeScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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