一、接口定义

  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 服务的三种生命周期的更多相关文章

  1. Autofac三种生命周期

    InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...

  2. Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency

    InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...

  3. Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency 【转载】

    InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...

  4. AutoFac使用方法总结三:生命周期

         生命周期 AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力.     AutoFac定义了三种生命周期: Per Dependency Single I ...

  5. IoC之AutoFac(三)——生命周期

    阅读目录 一.Autofac中的生命周期相关概念 二.创建一个新的生命周期范围 三.实例周期范围 3.1   每个依赖一个实例(InstancePerDependency) 3.2  单个实例(Sin ...

  6. Maven学习(三)生命周期

    maven有三套生命周期 1.clean       清理项目 2.default     构建项目 3.site           建立项目站点 每套生命周期都包含了一些阶段,这些阶段是有序的,后 ...

  7. Spring Environment(三)生命周期

    Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  8. 云计算服务的三种类型(SaaS、PaaS、IaaS)

    云计算可以帮助企业降低IT方面的成本和复杂性,并获得他们蓬勃发展所需的灵活性与敏捷性.但是,规划出通往云的明确路径并非易事.毕竟用户需要看透与云相关的市场大肆宣传,然后理解并分析不同种类的云计算模式的 ...

  9. Maven系列学习(三)Maven生命周期和插件

    Maven生命周期和插件 Maven另外的两个核心概念就是生命周期和插件,Maven的生命周期都是抽象的,其实实际行为都是由插件来完成的,生命周期和插件两者协同工作 1.生命周期 Maven的生命周期 ...

  10. 将【jar包、bat、其他文件】注册到windows服务的三种方法

    将[jar包.bat.其他文件]注册到windows服务的三种方法 1.instsrv.exe和srvany.exe 1.下载配置instsrv和srvany 下载地址:https://dl.pcon ...

随机推荐

  1. Node.js中的模块

    CommonJS模块 CommonJS是一种规范,它定义了JavaScript 在服务端运行所必备的基础能力,比如:模块化.IO.进程管理等.其中,模块化方案影响深远,其对模块的定义如下: 1,模块引 ...

  2. 韦东山IMX6ULL Linux开发板基于Buildroot系统QT应用环境配置开发运行

    @ 目录 一. 编译系统 1.设置交叉编译工具链 2.编译系统 二. QT下载 1.安装 Qtcreator 2.创建第一个程序 3.配置 QtCreator 开发环境 4.移植QT程序到开发板 一. ...

  3. 韦东山freeRTOS系列教程之【第九章】任务通知(Task Notifications)

    目录 系列教程总目录 概述 9.1 任务通知的特性 9.1.1 优势及限制 9.1.2 通知状态和通知值 9.2 任务通知的使用 9.2.1 两类函数 9.2.2 xTaskNotifyGive/ul ...

  4. vulnhub - LAMPSECURITY: CTF5

    vulnhub - LAMPSECURITY: CTF5 信息收集 端口扫描 nmap -sT --min-rate 10000 -p- 192.168.157.164 详细扫描 sudo nmap ...

  5. 使用bootstrap-select 动态加载数据不显示的问题,级联数据置为空

    动态加载数据 $.showLoading('数据加载中');//开启遮挡层 $.ajax({ url: "/PickoutStock/GetSendReceive", data: ...

  6. 劫持TLS绕过canary && 堆和栈的灵活转换

    引入:什么是TLScanary? TLScanary 是一种在 Pwn(主要是二进制漏洞利用)中常见的技术,专门用于处理 TLS 保护的二进制文件.在安全竞赛(例如 CTF)和漏洞利用场景中,攻击者需 ...

  7. Solo 开发者周刊 (第5期):打破常规,探索技术新边界

    这里会整合 Solo 社区每周推广内容.产品模块或活动投稿,每周五发布.在这期周刊中,我们将深入探讨开源软件产品的开发旅程,分享来自一线独立开发者的经验和见解.本杂志开源,欢迎投稿. 产品推荐 1. ...

  8. C#事件总结(二)

    续接上一篇文<C#事件总结>,那是通过一个结合例子的文字描述,接下来我将通过图文形式展现. 现在VS开发环境中看看事件是什么样子的: 再来看看我的笔记,希望对你的理解有帮助. 哦,顺便提醒 ...

  9. PowerBuilder编程新思维6.5:外传1(PowerPlume的设计与规划)

    <第五部分 Otherside 意外的宝藏> 每一颗种子都有发芽的梦想.PowerPlume(孔雀翎)开发交流群:286502392    PowerBuilder编程新思维6.5:外传1 ...

  10. ArchLinux Vmware安装指北

    ArchLinux Vmware安装指北 在本文开始之前,首先允许我提前声明一点,Arch Linux的安装并不算难,但是绝对也算不上简单,中间的安装可能会遇到很多问题,本篇文章不能保证完全贴合你的真 ...