简体   繁体   English

ASP.NET 5中的依赖注入和对象处理

[英]Dependency Injection in the ASP.NET 5 and Object Dispose

Can anybody help me to understand following in context to Dependency Injection in Asp.Net 5 and object dispose. 任何人都可以帮助我了解Asp.Net 5中依赖注入和对象配置的上下文。

I need to understand if my Service implements IDispose interface ,who will call dispose method. 我需要了解我的服务是否实现IDispose接口,谁会调用dispose方法。

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IService, Service>();
    services.AddScoped<IService, Service>();
    services.AddSingleton<IService, Service>();
    services.AddInstance<IService, Service>();
}         

IServiceCollection contains the set of services available in your application. IServiceCollection包含应用程序中可用的服务集。 You defines the services you want to use and their lifetime, and the application will instantiate and dispose them for you. 您定义了要使用的服务及其生存期,应用程序将实例化并为您配置它们。

There are 4 different lifetimes : 有4个不同的生命周期:

Transient 短暂的

Transient lifetime services are created each time they are requested. 每次请求时都会创建瞬态生存期服务。 This lifetime works best for lightweight, stateless service. 此生命周期最适合轻量级,无状态服务。

Scoped 范围

Scoped lifetime services are created once per request. 每个请求一次创建范围的生命周期服务。

Singleton 辛格尔顿

Singleton lifetime services are created the first time they are requested, and then every subsequent request will use the same instance. 单例生命周期服务是在第一次请求它们时创建的,然后每个后续请求将使用相同的实例。 If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself. 如果您的应用程序需要单例行为,则建议允许服务容器管理服务的生命周期,而不是自己在类中实现单例设计模式并管理对象的生命周期。

Instance 实例

You can choose to add an instance directly to the services container. 您可以选择将实例直接添加到服务容器。 If you do so, this instance will be used for all subsequent requests (this technique will create a Singleton-scoped instance). 如果这样做,则此实例将用于所有后续请求(此技术将创建一个Singleton范围的实例)。 One key difference between Instance services and Singleton services is that the Instance service is created in ConfigureServices, while the Singleton service is lazy-loaded the first time it is requested. 实例服务和Singleton服务之间的主要区别在于,实例服务是在ConfigureServices中创建的,而Singleton服务是在第一次请求时被延迟加载的。

The asp.net 5 official documentation is great, take time to read it : http://docs.asp.net/en/latest/fundamentals/dependency-injection.html asp.net 5官方文档很棒,请花点时间阅读: http : //docs.asp.net/en/latest/fundamentals/dependency-injection.html


The documentation doesn't mention how exactly the dependencies lifetimes are handled by the dependency injection service but if you search in the code, you will find the ServiceProvider class, who manages the lifetimes : ServiceManager class 该文档没有提到依赖项注入服务如何精确地处理依赖项生存期,但是如果您在代码中进行搜索,则会发现管理生命周期的ServiceProvider类: ServiceManager类

To be a little more specific, when a scope is created, the service scope factory returns a new service scope, who is instanciated with a service provider. 更具体地说,在创建范围时, 服务范围工厂将返回一个新的服务范围,该范围由服务提供者实例化。 When the dependency injection service have to dispose a service, he calls the service scope's dispose method , who calls the service provider's dispose method . 当依赖项注入服务必须处理服务时,他调用服务范围的dispose方法 ,后者又调用服务提供者的dispose方法

How the service provider works ? 服务提供商如何工作? He has all the service scopes in a property, named _resolvedServices , and all the transiant disposables in a property named _transientDisposables . 他在名为_resolvedServices的属性中具有所有服务范围,并在名为_transientDisposables的属性中具有所有可替换一次性对象 When the dispose() method of the service provider is called, he loops on all the items he has in this two properties, and for each object calls his dispose method. 调用服务提供者dispose()方法时 ,他将循环访问他在这两个属性中拥有的所有项目,并为每个对象调用他的dispose方法。

You have all the source code here : Dependency Injection source code 您在这里拥有所有源代码: 依赖注入源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM