.NETCore 服务的三种生命周期
一、接口定义
public interface ITestSerivceSingleton
{
public string GetServiceNameBase() {
return "ITestSerivceSingleton";
} public string GetServiceName();
} public interface ITestSerivceScoped
{
public string GetServiceNameBase()
{
return "ITestSerivceScoped";
} public string GetServiceName(); } public interface ITestSerivceTransient
{
public string GetServiceNameBase() {
return "ITestSerivceTransient";
} public string GetServiceName();
}
二、实现接口
public class TestSerivceScoped : ITestSerivceScoped
{
public string Name { get; set; } public TestSerivceScoped()
{
Name = Guid.NewGuid().ToString();
}
public string GetServiceName()
{
return "TestSerivceScoped" + Name;
}
} public class TestSerivceSingleton : ITestSerivceSingleton
{
public string Name { get; set; } public TestSerivceSingleton()
{
Name = Guid.NewGuid().ToString();
}
public string GetServiceName()
{
return "TestSerivceSingleton" +Name;
}
} public class TestSerivceTransient : ITestSerivceTransient
{
public string Name { get; set; } public TestSerivceTransient()
{
Name = Guid.NewGuid().ToString();
}
public string GetServiceName()
{
return "TestSerivceTransient" + Name;
}
}
三、服务注册(.NetCore自带IOC容器)
builder.Services.AddTransient<ITestSerivceTransient,TestSerivceTransient>();
builder.Services.AddScoped<ITestSerivceScoped, TestSerivceScoped>();
builder.Services.AddSingleton<ITestSerivceSingleton, TestSerivceSingleton>();
四、测试作用域
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly ITestSerivceSingleton testSerivceSingleton;
private readonly ITestSerivceScoped testSerivceScoped;
private readonly ITestSerivceTransient testSerivceTransient;
private readonly ITestSerivceSingleton testSerivceSingleton1;
private readonly ITestSerivceScoped testSerivceScoped1;
private readonly ITestSerivceTransient testSerivceTransient1;
public HomeController(ITestSerivceSingleton testSerivceSingleton,ITestSerivceScoped testSerivceScoped,ITestSerivceTransient testSerivceTransient,
ITestSerivceSingleton testSerivceSingleton1, ITestSerivceScoped testSerivceScoped1, ITestSerivceTransient testSerivceTransient1) {
this.testSerivceSingleton = testSerivceSingleton;
this.testSerivceScoped = testSerivceScoped;
this.testSerivceTransient = testSerivceTransient;
this.testSerivceSingleton1 = testSerivceSingleton1;
this.testSerivceScoped1 = testSerivceScoped1;
this.testSerivceTransient1 = testSerivceTransient1;
}
[HttpGet("Index")]
public IActionResult Index()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine(testSerivceSingleton.GetServiceNameBase());
stringBuilder.AppendLine(testSerivceSingleton.GetServiceName());
stringBuilder.AppendLine(testSerivceSingleton1.GetServiceName());
stringBuilder.AppendLine(testSerivceScoped.GetServiceNameBase());
stringBuilder.AppendLine(testSerivceScoped.GetServiceName());
stringBuilder.AppendLine(testSerivceScoped1.GetServiceName());
stringBuilder.AppendLine(testSerivceTransient.GetServiceNameBase());
stringBuilder.AppendLine(testSerivceTransient.GetServiceName());
stringBuilder.AppendLine(testSerivceTransient1.GetServiceName());
return Content(stringBuilder.ToString());
}
}
五、测试结果

六、验证结论
Singleton(单例服务):每次请求都是同一个服务实例
Scoped(作用域服务):同一次请求时同一个服务实例,不同请求服务实例不同
Transient(瞬时服务):同一次或不同请求中每次使用的服务实例都是新的实例
当Singleton中包含Scoped、Transient成员,Scoped、Transient成员生命周期会改变
Scoped、Transient中包含Singleton,Singleton成员生命周期不变
单例对象会改变成员的生命周期
单例对象生命周期无法改变
.NETCore 服务的三种生命周期的更多相关文章
- Autofac三种生命周期
InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...
- Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency
InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...
- Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency 【转载】
InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...
- AutoFac使用方法总结三:生命周期
生命周期 AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力. AutoFac定义了三种生命周期: Per Dependency Single I ...
- IoC之AutoFac(三)——生命周期
阅读目录 一.Autofac中的生命周期相关概念 二.创建一个新的生命周期范围 三.实例周期范围 3.1 每个依赖一个实例(InstancePerDependency) 3.2 单个实例(Sin ...
- Maven学习(三)生命周期
maven有三套生命周期 1.clean 清理项目 2.default 构建项目 3.site 建立项目站点 每套生命周期都包含了一些阶段,这些阶段是有序的,后 ...
- Spring Environment(三)生命周期
Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...
- 云计算服务的三种类型(SaaS、PaaS、IaaS)
云计算可以帮助企业降低IT方面的成本和复杂性,并获得他们蓬勃发展所需的灵活性与敏捷性.但是,规划出通往云的明确路径并非易事.毕竟用户需要看透与云相关的市场大肆宣传,然后理解并分析不同种类的云计算模式的 ...
- Maven系列学习(三)Maven生命周期和插件
Maven生命周期和插件 Maven另外的两个核心概念就是生命周期和插件,Maven的生命周期都是抽象的,其实实际行为都是由插件来完成的,生命周期和插件两者协同工作 1.生命周期 Maven的生命周期 ...
- 将【jar包、bat、其他文件】注册到windows服务的三种方法
将[jar包.bat.其他文件]注册到windows服务的三种方法 1.instsrv.exe和srvany.exe 1.下载配置instsrv和srvany 下载地址:https://dl.pcon ...
随机推荐
- Windows安装OnlyOfiice教程
1.OnlyOffice介绍 OnlyOffice 是一个在线创建.编辑和协作文档的服务. 2.Docker介绍 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移 ...
- Luogu P5745 区间最大和
原题链接:Luogu P5745 区间最大和 初看这道题,
- saltStack自动化工具
目录 SaltStack自动化工具 核心概念 1. Master 和 Minion 2. State 3. Pillar 4. Grains 5. Modules 6. Runner 7. React ...
- TP5.0学习笔记
TP5目录结构介绍 application目录是应用目录,我们整个应用所有的内容都写在这个目录中,在后续开发中,我们更多的时候都是在编写这个目录中的文件.在它里边有一个index文件夹,它叫做模块儿, ...
- [oeasy]python0097_苹果诞生_史蒂夫_乔布斯_沃兹尼亚克_apple_I
苹果诞生 回忆上次内容 上次时代华纳公司 凭借手中的影视ip和资本 吞并了雅达利公司 此时 雅达利公司 曾经开发过pong的 优秀员工 乔布斯 还在 印度禅修 寻找自我 看到游戏行业 蓬勃发展 乔布斯 ...
- 题解:AT_arc173_b [ARC173B] Make Many Triangles
背景 前几天打了比赛,崩麻了,所以来水一篇题解.LC真睿智 题意 给你 \(n\) 个点,问最多能组成几个三角形. 分析 听说可以随机化.这道题就是一个简单贪心. 我们考虑,如果没有共线的点,那么答案 ...
- Vue 新增不参与打包的接口地址配置文件
Vue 新增不参与打包的接口地址配置文件 by:授客 QQ:1033553122 开发环境 Win 10 Vue 2.5.2 问题描述 vue工程项目,npm run build we ...
- __int128的输入输出(快读快输)
引言:__int128不能用\(cin\)\(cout\)或\(scanf\)\(printf\). 快读 思想:把每一个字符读入,组成数字. int read(){ int x = 0,y = 1; ...
- 【AppStore】IOS应用上架Appstore的一些小坑
前言 上一篇文章写到如何上架IOS应用到Appstore,其中漏掉了些许期间遇到的小坑,现在补上 审核不通过原因 5.1.1 Guideline 5.1.1 - Legal - Privacy - D ...
- TCP和KCP协议
TCP协议 KCP是一个快速可靠协议,能以比 TCP 浪费 10%-20% 的带宽的代价,换取平均延迟降低 30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发 ...