最近在做一个小的Demo中,在一个界面上两次调用视图组件,并且在视图组件中都调用了数据库查询,结果发现,一直报错,将两个视图组件的调用分离,单独进行,却又是正常的,寻找一番,发现是配置依赖注入服务时,对于服务的生命周期没有配置得当导致,特此做一次实验来认识三者之间(甚至是四者之间的用法及区别)。
本文demo地址(具体见WebApi控制器中):https://gitee.com/530521314/koInstance.git (本地下载)
一、服务的生命周期
在Asp.Net Core中,内置容器负责管理服务的生命周期,从被依赖注入容器创建开始,等我们调用完服务时,到容器释放该服务的所有实力为止,有几种形式表现:
1、Transient:每次请求服务时,都会创建一个新实例,这种生命周期适合用于轻量级服务(如Repository和ApplicationService服务)。
2、Scoped:为每个HTTP请求创建一个实例,生命周期将横贯整次请求。
3、SingleTon:在第一次请求服务时,为该服务创建一个实例,之后每次请求将会使用第一次创建好的服务。
4、Instance:与SingleTon类似,但在应用程序启动时会将该实例注册到容器中,可以理解为比SingleTon还早存在。
应用程序中相关服务的控制生命周期的方法时通过相应的Add*指定,如下三种,当然还可以通过扩展方法来简化ConfigurationServices方法中所见的代码数量。
services.AddTransient<IApplicationService, ApplicationService>(); services.AddScoped<IApplicationService, ApplicationService>(); services.AddSingleton<IApplicationService, ApplicationService>();
二、代码设计服务生命周期
首先设计一些服务相关的操作接口
public interface IOperation { Guid GetGuid(); } public interface IOperationTransient: IOperation { } public interface IOperationScoped : IOperation { } public interface IOperationSingleton : IOperation { } public interface IOperationInstance : IOperation { } 基础服务接口
其次对这些操作类予以实现并生成相关服务
/// <summary> /// 常规服务 /// </summary> public class Operation : IOperation { private readonly Guid _guid; public Operation() { _guid = Guid.NewGuid(); } public Operation(Guid guid) { _guid = guid == Guid.Empty ? Guid.NewGuid() : guid; } public Guid GetGuid() { return _guid; } } /// <summary> /// 瞬时服务 /// </summary> public class OperationTransient : IOperationTransient { private readonly Guid _guid; public OperationTransient() { _guid = Guid.NewGuid(); } public OperationTransient(Guid guid) { _guid = guid == Guid.Empty ? Guid.NewGuid() : guid; } public Guid GetGuid() { return _guid; } } /// <summary> /// 单次请求内服务固定 /// </summary> public class OperationScoped : IOperationScoped { private readonly Guid _guid; public OperationScoped() { _guid = Guid.NewGuid(); } public OperationScoped(Guid guid) { _guid = guid == Guid.Empty ? Guid.NewGuid() : guid; } public Guid GetGuid() { return _guid; } } /// <summary> /// 所有请求内固定服务 /// </summary> public class OperationSingleton : IOperationSingleton { private readonly Guid _guid; public OperationSingleton() { _guid = Guid.NewGuid(); } public OperationSingleton(Guid guid) { _guid = guid == Guid.Empty ? Guid.NewGuid() : guid; } public Guid GetGuid() { return _guid; } } /// <summary> /// 应用程序内固定服务 /// </summary> public class OperationInstance : IOperationInstance { private readonly Guid _guid; public OperationInstance() { _guid = Guid.NewGuid(); } public OperationInstance(Guid guid) { _guid = guid == Guid.Empty ? Guid.NewGuid() : guid; } public Guid GetGuid() { return _guid; } } 基础服务具体实现
对基础服务的聚合接口,提供统一服务接口
public interface IOperationService { /// <summary> /// 获取四种形式的Guid码 /// </summary> /// <returns></returns> List<string> GetGuidString(); } 聚合服务接口
对基础服务的聚合实现,将基础服务全部接入进来作为统一服务
/// <summary> /// 服务调用 /// </summary> public class OperationService : IOperationService { public IOperationTransient _transientOperation { get; } public IOperationScoped _scopedOperation { get; } public IOperationSingleton _singletonOperation { get; } public IOperationInstance _instanceOperation { get; } public OperationService(IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationInstance instanceOperation) { _transientOperation = transientOperation; _scopedOperation = scopedOperation; _singletonOperation = singletonOperation; _instanceOperation = instanceOperation; } public List<string> GetGuidString() { return new List<string>() { $"Transient:"+_transientOperation.GetGuid(), $"Scoped:"+_scopedOperation.GetGuid(), $"Singleton:" +_singletonOperation.GetGuid(), $"Instance:"+_instanceOperation.GetGuid(), }; } } 聚合服务的实现
在控制器中进行服务注入