.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 ...
随机推荐
- 颠覆传统编程,用ChatGPT十倍提升生产力
我们即将见证一个新的时代!这是最好的时代,也是最坏的时代! 需求背景 背景: 平时会编写博客,并且会把这个博客上传到github上,然后自己买一个域名挂到github上. 我平时编写的博客会有一些图片 ...
- (数据科学学习手札162)Python GIS神器geopandas 1.0版本发布
本文完整代码及附件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在昨天,Python生 ...
- python重拾第十一天-RABBITMQ队列
安装python rabbitMQ module pip install pika 官网 https://pypi.python.org/pypi/pika 安装rabbit-server服务,cen ...
- python基础-字符串str " "
字符串的定义和操作 字符串的特性: 元素数量 支持多个 元素类型 仅字符 下标索引 支持 重复元素 支持 可修改性 不支持 数据有序 是 使用场景 一串字符的记录场景 字符串的相关操作: my_str ...
- DAX 自动生成日期表-与订单表(业绩表)相同日期区间
日期表 = ADDCOLUMNS ( CALENDAR (MIN('业绩表'[日期]), MAX('业绩表'[日期])), //关键在于MIN函数和MAX函数的使用 "年度", Y ...
- mysql求同比环比
-- 参考:SQL计算月环比.月同比_路易吃泡面的博客-CSDN博客 -- mysql同比环比 drop table if EXISTS ordertable; create table ordert ...
- test20230824总结
A 怎么是重构树板子,放在图上都是水题. B 考场上只打了一个暴力,赛后发现似乎是很可做的 C 是一个考察状态设计的 dp 以后要多刷 D 是一道数据结构优化 dp 考场上写出来了却因为空间问题挂了 ...
- TCP/UDP 协议和 HTTP/FTP/SMTP 协议之间的区别
前言 我们经常会听到HTTP协议.TCP/IP协议.UDP协议.Socket.Socket长连接.Socket连接池等字眼,然而它们之间的关系.区别及原理并不是所有人都能理解清楚. 计算机网络体系结构 ...
- 英特尔开源新等宽字体Intel One Mono,称可保护开发者视力
英特尔开源了一款面向开发者的新等宽字体 "Intel One Mono ",这是一种富有表现力的等宽字体系列,集清晰度.易读性和开发者视力保护于一体. Intel One Mono ...
- 学习笔记--Java方法中的注意事项
Java方法中的注意事项 方法调用 Java的方法在同一个类中调用,可以省略 类名. /* 方法调用 */ public class MethodTest03{ public static void ...