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