Net Core 三个常用的生命周期

AddTransient    瞬间生命周期   :  每次需要创建一个全新的

AddSigleton      单例进程唯一实例    :每一次请求都是同一个(多用于时间)

AddScoped      作用域单例    :一个请求,一个实例;不同请求,不同实例。

一次的http请求 就是一个实例。

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>();

猜你喜欢

转载自www.cnblogs.com/ZkbFighting/p/12922029.html