.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成员生命周期不变
单例对象会改变成员的生命周期
单例对象生命周期无法改变
喜欢 C#、SQL、Web