From 94763a949b48105996b5279a5e78c3edfd50971b Mon Sep 17 00:00:00 2001 From: Jadyn Date: Mon, 25 Sep 2023 17:15:20 +0800 Subject: [PATCH 1/2] Imporve console template with `HostApplicationBuilder` --- .../AbpConfigurationExtensions.cs | 18 +++++++++++ .../MyProjectNameHostedService.cs | 8 ++--- .../MyCompanyName.MyProjectName/Program.cs | 32 +++++++++++++------ .../Properties/launchSettings.json | 10 ++++++ 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs create mode 100644 templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs new file mode 100644 index 0000000000..6e0a14fd94 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.Hosting; + +namespace Microsoft.Extensions.Configuration; + +public static class AbpConfigurationExtensions +{ + public static IConfigurationBuilder AddAppSettingsSecretsJson( + this IConfigurationBuilder builder, + bool optional = true, + bool reloadOnChange = true, + string path = AbpHostingHostBuilderExtensions.AppSettingsSecretJsonPath) + { + return builder.AddJsonFile(path: path, optional: optional, reloadOnChange: reloadOnChange); + } +} diff --git a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs index c31bcc282f..3876cac7e2 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs @@ -7,13 +7,11 @@ namespace MyCompanyName.MyProjectName; public class MyProjectNameHostedService : IHostedService { - private readonly IAbpApplicationWithExternalServiceProvider _abpApplication; private readonly HelloWorldService _helloWorldService; - public MyProjectNameHostedService(HelloWorldService helloWorldService, IAbpApplicationWithExternalServiceProvider abpApplication) + public MyProjectNameHostedService(HelloWorldService helloWorldService) { _helloWorldService = helloWorldService; - _abpApplication = abpApplication; } public async Task StartAsync(CancellationToken cancellationToken) @@ -21,8 +19,8 @@ public class MyProjectNameHostedService : IHostedService await _helloWorldService.SayHelloAsync(); } - public async Task StopAsync(CancellationToken cancellationToken) + public Task StopAsync(CancellationToken cancellationToken) { - await _abpApplication.ShutdownAsync(); + return Task.CompletedTask; } } diff --git a/templates/console/src/MyCompanyName.MyProjectName/Program.cs b/templates/console/src/MyCompanyName.MyProjectName/Program.cs index 79d12b850f..5d9b118aac 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/Program.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/Program.cs @@ -1,10 +1,14 @@ using System; 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; @@ -28,19 +32,27 @@ public class Program { Log.Information("Starting console host."); - var builder = Host.CreateDefaultBuilder(args); + var builder = Host.CreateApplicationBuilder(args); - builder.ConfigureServices(services => - { - services.AddHostedService(); - services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(services.GetConfiguration()); - }); - }).AddAppSettingsSecretsJson().UseSerilog().UseAutofac().UseConsoleLifetime(); + builder.Configuration.AddAppSettingsSecretsJson(); + builder.Logging.ClearProviders().AddSerilog(); + + builder.Services.AddAutofacServiceProviderFactory(); + + builder.Services.AddSingleton(); + + builder.Services.AddHostedService(); + await builder.Services.AddApplicationAsync(); var host = builder.Build(); - await host.Services.GetRequiredService().InitializeAsync(host.Services); + + 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.RunAsync(); diff --git a/templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json b/templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json new file mode 100644 index 0000000000..99027a4bbc --- /dev/null +++ b/templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "MyCompanyName.MyProjectName": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file From b8c75d21f1730a0a3bdafbb1a4aed1088504a918 Mon Sep 17 00:00:00 2001 From: Jadyn Date: Wed, 27 Sep 2023 12:50:36 +0800 Subject: [PATCH 2/2] 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();