隐藏页面特效

.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成员生命周期不变

 

单例对象会改变成员的生命周期

单例对象生命周期无法改变

 


__EOF__

本文作者DaiWK
本文链接https://www.cnblogs.com/daiwk/p/16071855.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   DaiWK  阅读(436)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示