.netcore运用IServiceScope实现全局服务持续化注入 电脑版发表于:2020/4/13 11:04 ># IServiceScope实现全局服务持续化注入 [TOC] ###### 创建 GloablePullWorkFlow.cs 类 ------------ ```csharp public class GloablePullWorkFlow { public static IServiceScope Scope { get; set; } } ``` ###### 对 Program.cs 下做如下修改 ------------ ```csharp public static void Main(string[] args) { var host = BuildWebHost(args); GloablePullWorkFlow.Scope = host.Services.CreateScope(); var _logger = GloablePullWorkFlow.Scope.ServiceProvider.GetService<Microsoft.Extensions.Logging.ILogger<Program>>(); try { } catch (Exception e) { _logger.LogInformation(e.Message); } host.Run(); } ``` <br/> 这样通过`var _name = GloablePullWorkFlow.Scope.ServiceProvider.GetService<ServiceName>();`来进行服务注入