From b8c75d21f1730a0a3bdafbb1a4aed1088504a918 Mon Sep 17 00:00:00 2001 From: Jadyn Date: Wed, 27 Sep 2023 12:50:36 +0800 Subject: [PATCH] Improve Move application initialize method to Volo.Abp.Core --- .../Extensions/Hosting/AbpHostExtensions.cs | 20 +++++++++++++++++++ .../MyCompanyName.MyProjectName/Program.cs | 15 +++----------- 2 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs new file mode 100644 index 0000000000..12c03e14f5 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; +using Volo.Abp.Threading; + +namespace Microsoft.Extensions.Hosting; + +public static class AbpHostExtensions +{ + public static async Task InitializeAsync(this IHost host) + { + var application = host.Services.GetRequiredService(); + var applicationLifetime = host.Services.GetRequiredService(); + + applicationLifetime.ApplicationStopping.Register(() => AsyncHelper.RunSync(() => application.ShutdownAsync())); + applicationLifetime.ApplicationStopped.Register(() => application.Dispose()); + + await application.InitializeAsync(host.Services); + } +} diff --git a/templates/console/src/MyCompanyName.MyProjectName/Program.cs b/templates/console/src/MyCompanyName.MyProjectName/Program.cs index 5d9b118aac..dd1ce382c9 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/Program.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/Program.cs @@ -3,12 +3,10 @@ using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Hosting.Internal; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events; using Volo.Abp; -using Volo.Abp.Threading; namespace MyCompanyName.MyProjectName; @@ -37,22 +35,15 @@ public class Program builder.Configuration.AddAppSettingsSecretsJson(); builder.Logging.ClearProviders().AddSerilog(); - builder.Services.AddAutofacServiceProviderFactory(); - - builder.Services.AddSingleton(); + builder.ConfigureContainer(builder.Services.AddAutofacServiceProviderFactory()); builder.Services.AddHostedService(); + await builder.Services.AddApplicationAsync(); var host = builder.Build(); - var application = host.Services.GetRequiredService(); - var applicationLifetime = host.Services.GetRequiredService(); - - applicationLifetime.ApplicationStopping.Register(() => AsyncHelper.RunSync(() => application.ShutdownAsync())); - applicationLifetime.ApplicationStopped.Register(() => application.Dispose()); - - await application.InitializeAsync(host.Services); + await host.InitializeAsync(); await host.RunAsync();