Autofac - InstancePerHtt prequest VS InstancePerLifetimeScope [英] Autofac - InstancePerHttpRequest vs InstancePerLifetimeScope

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

问题描述

什么是两个范围之间的区别是什么?

What are the differences between the two scopes?

我建立模块(S)在每个层(库,服务,MVC应用程序),但为了有 InstancePerHtt prequest 您需要的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?

推荐答案

InstancePerHtt prequest InstancePerApiRequest 基本上做同样的事情 - 你得到你的每一个独立的Web请求服务的一个实例。我将使用 InstancePerHtt prequest 的答案的其余部分,但要记住,这两个是可以互换的。

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.

唯一真正的区别在于,如果你有在 InstancePerHtt prequest 注册的服务,您请求从被注册为<$其他服务的服务之一C $ C> 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 组件住在范围

  • InstancePerHtt prequest 部分住在一个叫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 服务无法找到 InstancePerHtt prequest 服务。

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

但是,如果在这种情况下,你已经使用 InstancePerLifetimeScope (而不是 InstancePerHtt prequest ),然后你的服务将能够解决就好了。

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

我已经写了下载code相当详尽的文章,试图解释这一切细节 - 的在这里看到。从文章引用:

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请求的范围 - 即是终身是指网页请求的生命周期。正如你所看到的,这是假的。

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.

希望帮助! - 我AP preciate这个问题现在已经很老了,但是它还是很相关的

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

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

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