这篇文章将为大家详细讲解有关如何在Asp.Net Core中使用生命周期选项,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
一、服务的生命周期
在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(),
};
}
}
聚合服务的实现
在控制器中进行服务注入
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly IOperationService _operationService;
public ValuesController(IOperationService operationService)
{
_operationService = operationService;
}
[HttpGet]
[Route(nameof(GetGuidString))]
public ActionResult<string> GetGuidString()
{
return string.Join("\n", _operationService.GetGuidString());
}
}
在StartUp中完成服务注入逻辑,这里实现服务注入的方式多种均可。
services.AddTransient<IOperationTransient, OperationTransient>();
services.AddScoped<IOperationScoped, OperationScoped>();
services.AddSingleton<IOperationSingleton, OperationSingleton>();//应用程序启动时便注入该实例
services.AddSingleton<IOperationInstance>(new OperationInstance(Guid.Empty));
services.AddTransient<IOperationService, OperationService>();
通过访问预期Api地址可以得到不同的四种基础服务的Guid信息,
第一次启动程序(不关闭)发起访问:
第二次(第一次基础上再次访问)发起访问:
可以看见,两次访问下,Singleton和Instance是相同的,都是由应用程序启动时和应用服务加载时决定完毕,Singleton在首次进入服务时进行分配,并始终保持不变,而Instance在应用程序启动时,便将实例注入,进入服务也保持着最先的实例,没有重新分配实例。而Transient和Scoped则进行着变化。
关闭程序,重启,第三次发起访问:
可以见到,Singleton和Instance都发生了变化,也说明了之前在Singleton和Instance处写上的作用。
接下来开始设计Transient和Scoped的不同之处,对于已有代码加上新功能,此次我们只针对Scoped和Transient进行比较。
首先在StartUp中将HttpContextAccessor服务注入,目的是在后期能够针对Scoped获取新的服务实例(尽管两个实例是相同的)。
services.AddHttpContextAccessor();
接着在聚合服务中增加一个方法,用来针对Transient、Scoped测试。
/// <summary>
/// 获取Transient、Scoped的Guid码
/// </summary>
/// <returns></returns>
List<string> GetTransientAndScopedGuidString();
在聚合服务实现中实现该方法并对已有的服务重新获取实例,得到不同实例下的Guid码。
public List<string> GetTransientAndScopedGuidString()
{
//var tempTransientService = (IOperationTransient)ServiceLocator.Instance.GetService(typeof(IOperationTransient));
var tempTransientService = (IOperationTransient)_httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IOperationTransient));
var tempScopedService = (IOperationScoped)_httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IOperationScoped));
return new List<string>()
{
$"原生Transient请求服务:"+_transientOperation.GetGuid(),
$"手动Transient请求服务:"+ tempTransientService.GetGuid(),
$"原生Scoped请求服务:"+_scopedOperation.GetGuid(),
$"手动Scoped请求服务:"+tempScopedService.GetGuid(),
};
}
在控制器部分调用该聚合服务即可,并返回相应的结果,本次我返回的结果:
ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。
关于如何在Asp.Net Core中使用生命周期选项就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。