.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 ...
随机推荐
- 了解Microsoft Media Foundation
关于Microsoft Media Foundation 是什么 Microsoft Media Foundation是用来处理(创建.修改.传输.合成)多媒体数据(音视频)的一个平台. 有什么用 M ...
- Linux 使用 Swap分区
Linux 使用 Swap分区 背景 买的云服务器在使用的时候,资源经常不够,因此需要使用swap分区. Swap分区在系统的物理内存不够用的时候,把硬盘内存中的一部分空间释放出来,以供当前运行的程序 ...
- 解码技术债:AI代码助手与智能体的革新之道
技术债 技术债可能来源于多种原因,比如时间压力.资源限制.技术选型不当等.它可以表现为代码中的临时性修补.未能彻底解决的设计问题.缺乏文档或测试覆盖等.虽然技术债可以帮助快速推进项目进度,但长期来看, ...
- Linux 中 WIFI 和热点的使用
之前一直在 ubuntu 的图形界面中使用,突然需要在 ARM 板上打开热点,一时给弄蒙了,在此记录一下 一.网卡命令 显示所有网络信息 sudo ip link show 关闭或打开网络 sudo ...
- 1.1 第一个hello程序
还记得在每一个编程平台上的第一个程序都是hello world,现在就以这个程序为载体,先浅聊一下计算机系统吧. 1.预处理阶段,预处理器cpp根据字符#开头的命令修改原始的程序,并把头文件里的内容直 ...
- 首届 DIVE 精彩回顾丨践行企业数字化,基础软件如何创新
"墙高基下,虽得必失."在构建数字企业大厦的工程中,基础软件的重要性不言而喻.但对于各行各业而言,面向传统经营模式设计的基础软件已经难以支撑数字业务的创新,唯有汲取专业团队的经验, ...
- Three光源Target位置改变光照方向不变的问题及解决方法
0x00 楔子 在 Three.js 中,光源的目标(target)是一种用于指定光源方向的重要元素.在聚光灯中和定向光(DirectionalLight)中都有用到. 有时我们可能会遇到光源目标位置 ...
- mysql大数据表添加字段
方案一.老表数据迁移四部曲方案1.新建老表t_order_goods的备份表t_order_goods_bak,同时加一个字段:isVirtual 并给默认值2.迁移老表t_order_goods数据 ...
- ASP.NET Core WebAPI 使用CreatedAtRoute通知消费者
一.目的 我想告诉消费者我的api关于新创建的对象的位置 二.方法说明 public virtual Microsoft.AspNetCore.Mvc.CreatedAtRouteResult Cre ...
- P10507 Georgia and Bob 题解
思路 对棋子坐标排序,\(x_{i}-x_{i-1}-1\) 就是棋子可以移动的距离. 移动第 \(i\) 个棋子,相当于将 \(i+1\) 的移动范围扩大. 这于是变形成了一个台阶 nim 博弈论. ...