.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 ...
随机推荐
- python重拾第十一天-RABBITMQ队列
安装python rabbitMQ module pip install pika 官网 https://pypi.python.org/pypi/pika 安装rabbit-server服务,cen ...
- Android/SELinux 添加 AVC 权限
Android/SELinux 添加 AVC 权限 背景 在Android应用层中编写c/c++应用时,发现接口调用出现问题,logcat才知道是因为:权限不够. type=1400 audit(0. ...
- ZYNQ:使用 PetaLinux 构建Linux项目
参考文档:ug1144-petalinux-tools-reference-guide.pdf 环境安装 tofrodos iproute2 gawk gcc g++ git make net-too ...
- 量子算法抛转(以及Oracle函数初步)
接下来要接触量子算法了,我们会看到怎么利用量子并行机制和干涉原理.干涉在算法对结果进行测量求值时举足轻重. Deutsch-Jozsa 算法 DJ算法是量子算法的入门算法,就像编程界的"He ...
- python3 webssh
简介 webssh 是 一个简单的 Web 应用程序,用作 ssh 客户端来连接到 ssh 服务器.它是用Python编写的,基于tornado.paramiko和xterm.js.下面简单搭建一个网 ...
- @ConfigurationProperties 还能这样用
在编写项目代码时,我们要求更灵活的配置,更好的模块化整合.在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或 applicat ...
- Spring5.X的注解配置项目
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 基于FileZilla上传、下载服务器数据的方法
本文介绍FileZilla软件的下载.配置与使用方法. 在之前的博客中,我们提到了下载高分遥感影像数据需要用到FTP(文件传输协议,File Transfer Protocol)软件FileZ ...
- thinkphp模型hasOne、hasMany、belongsTo详解
在ThinkPHP框架中,hasOne.hasMany和belongsTo是用于定义模型间一对多(1:n).一对一(1:1)和多对一(n:1)关联关系的方法.以下是一些简单的示例来解释这些关系: 1. ...
- selenium获取验证码截图
获取验证码截图代码: 获取验证码代码: #!/user/bin/env python3 # -*- coding: utf-8 -*- import requests from selenium im ...