From 98c5d4d4ac348b7e3c9aeaee97b8c82c3068c91f Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 19 Jul 2022 10:28:17 +0800 Subject: [PATCH] Enhance Console template --- .../MyProjectNameHostedService.cs | 30 ++++--------------- .../MyCompanyName.MyProjectName/Program.cs | 25 +++++++++++----- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs index 7bf0787299..c31bcc282f 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs @@ -1,42 +1,24 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Serilog; using Volo.Abp; namespace MyCompanyName.MyProjectName; public class MyProjectNameHostedService : IHostedService { - private IAbpApplicationWithInternalServiceProvider _abpApplication; + private readonly IAbpApplicationWithExternalServiceProvider _abpApplication; + private readonly HelloWorldService _helloWorldService; - private readonly IConfiguration _configuration; - private readonly IHostEnvironment _hostEnvironment; - - public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment) + public MyProjectNameHostedService(HelloWorldService helloWorldService, IAbpApplicationWithExternalServiceProvider abpApplication) { - _configuration = configuration; - _hostEnvironment = hostEnvironment; + _helloWorldService = helloWorldService; + _abpApplication = abpApplication; } public async Task StartAsync(CancellationToken cancellationToken) { - _abpApplication = await AbpApplicationFactory.CreateAsync(options => - { - options.Services.ReplaceConfiguration(_configuration); - options.Services.AddSingleton(_hostEnvironment); - - options.UseAutofac(); - options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog()); - }); - - await _abpApplication.InitializeAsync(); - - var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService(); - - await helloWorldService.SayHelloAsync(); + await _helloWorldService.SayHelloAsync(); } public async Task StopAsync(CancellationToken cancellationToken) diff --git a/templates/console/src/MyCompanyName.MyProjectName/Program.cs b/templates/console/src/MyCompanyName.MyProjectName/Program.cs index 83a61ceead..73c824a82e 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/Program.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; +using Volo.Abp; namespace MyCompanyName.MyProjectName; @@ -27,13 +28,23 @@ public class Program { Log.Information("Starting console host."); - await Host.CreateDefaultBuilder(args) - .ConfigureServices(services => + var builder = Host.CreateDefaultBuilder(args); + + builder.ConfigureServices(services => + { + services.AddHostedService(); + services.AddApplicationAsync(options => { - services.AddHostedService(); - }) - .UseSerilog() - .RunConsoleAsync(); + options.Services.ReplaceConfiguration(services.GetConfiguration()); + options.UseAutofac(); + options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog()); + }); + }).UseConsoleLifetime(); + + var host = builder.Build(); + await host.Services.GetRequiredService().InitializeAsync(host.Services); + + await host.RunAsync(); return 0; } @@ -47,4 +58,4 @@ public class Program Log.CloseAndFlush(); } } -} +} \ No newline at end of file