/// <summary> /// 泛型主机注入 /// </summary> /// <param name="hostBuilder">泛型主机注入构建器</param> /// <param name="autoRegisterBackgroundService">是否自动注册 BackgroundService</param> /// <returns>IWebHostBuilder</returns> public static IHostBuilder Inject(this IHostBuilder hostBuilder, bool autoRegisterBackgroundService = true) { hostBuilder.ConfigureAppConfiguration((hostingContext, config) => { // 存储环境对象 InternalApp.HostEnvironment = hostingContext.HostingEnvironment; // 加载配置 InternalApp.AddConfigureFiles(config, InternalApp.HostEnvironment); }); // 自动注入 AddApp() 服务 hostBuilder.ConfigureServices(services => { // 添加主机启动停止监听 services.AddHostedService <AppHostedService>(); // 添加全局配置和存储服务提供器 InternalApp.InternalServices = services; // 初始化应用服务 services.AddHostApp(); // 自动注册 BackgroundService if (autoRegisterBackgroundService) { services.AddAppHostedService(); } }); return(hostBuilder); }
/// <summary> /// 非 Web 主机注入 /// </summary> /// <param name="hostBuilder"></param> /// <returns></returns> public static IHostBuilder Inject(this IHostBuilder hostBuilder) { hostBuilder.ConfigureAppConfiguration(configurationBuilder => { InternalApp.AddConfigureFiles(configurationBuilder); }); // 自动注入 AddApp() 服务 hostBuilder.ConfigureServices(services => { // 添加全局配置和存储服务提供器 InternalApp.InternalServices = services; // 初始化应用服务 services.AddApp(); }); return(hostBuilder); }