.net core服务的三个生命周期

1、Transient  瞬时,每个请求都会创建一个实例,同一个请求,使用多次server,用的不同实例。(轻量级无状态服务尽量用Transient)

2、Scoped  作用域,在同一个Scope内只初始化一个实例,同一个请求,使用多次server,就用的同一个实例。(日常用Scope)

3、Singleton:单例,整个应用程序生命周期内只创建一个实例 

 

AddSingleton

//设定时区
var timeid = Environment.GetEnvironmentVariable("TZ");
VMSDateTime.TimeZoneId = timeid;
services.AddSingleton<IVMSDateTime, VMSDateTime>();

AddScoped

//设置接口类
services.AddScoped<IAuthenticateService, TokenAuthenticationService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<Code.IProcessInterface, Code.Process>();
services.AddScoped<Code.IBackendServiceBus, Code.BackendServiceBus>();

 

文章内容参考自:https://www.cnblogs.com/ZkbFighting/p/12922029.html

具体实例参考:https://www.cnblogs.com/subendong/p/8969259.html

 

posted @ 2020-09-25 09:57  狼窝窝  阅读(1151)  评论(0)    收藏  举报