From dbb02fa744d214854305d958e13823ea618f40bf Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 24 Dec 2021 11:29:33 +0800 Subject: [PATCH] Update the Console and WPF template. --- .../HelloWorldService.cs | 16 ++++- .../MyProjectNameHostedService.cs | 58 ++++++++++------- .../MyProjectNameModule.cs | 18 ++++-- .../MyCompanyName.MyProjectName/Program.cs | 26 +++----- .../appsettings.json | 2 +- .../MyCompanyName.MyProjectName/App.xaml.cs | 63 ++++++++----------- .../HelloWorldService.cs | 13 +++- 7 files changed, 110 insertions(+), 86 deletions(-) diff --git a/templates/console/src/MyCompanyName.MyProjectName/HelloWorldService.cs b/templates/console/src/MyCompanyName.MyProjectName/HelloWorldService.cs index 7ddd618c3e..7c095b85ed 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/HelloWorldService.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/HelloWorldService.cs @@ -1,12 +1,22 @@ -using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; namespace MyCompanyName.MyProjectName; public class HelloWorldService : ITransientDependency { - public void SayHello() + public ILogger Logger { get; set; } + + public HelloWorldService() + { + Logger = NullLogger.Instance; + } + + public Task SayHelloAsync() { - Console.WriteLine("\tHello World!"); + Logger.LogInformation("Hello World!"); + return Task.CompletedTask; } } diff --git a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs index 822844e10e..318e5a5508 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs @@ -1,40 +1,56 @@ -using System; -using System.Threading; +using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Extensions.Hosting; +using Serilog.Extensions.Logging; using Volo.Abp; namespace MyCompanyName.MyProjectName; public class MyProjectNameHostedService : IHostedService { - private readonly IAbpApplicationWithExternalServiceProvider _application; - private readonly IServiceProvider _serviceProvider; - private readonly HelloWorldService _helloWorldService; - - public MyProjectNameHostedService( - IAbpApplicationWithExternalServiceProvider application, - IServiceProvider serviceProvider, - HelloWorldService helloWorldService) + private IAbpApplicationWithInternalServiceProvider _abpApplication; + + private readonly IConfiguration _configuration; + private readonly IHostEnvironment _hostEnvironment; + + public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment) { - _application = application; - _serviceProvider = serviceProvider; - _helloWorldService = helloWorldService; + _configuration = configuration; + _hostEnvironment = hostEnvironment; } - public Task StartAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { - _application.Initialize(_serviceProvider); + _abpApplication = await AbpApplicationFactory.CreateAsync(options => + { + options.Services.ReplaceConfiguration(_configuration); + options.Services.AddSingleton(_hostEnvironment); + + options.UseAutofac(); - _helloWorldService.SayHello(); + // UseSerilog() + options.Services.AddLogging(); + options.Services.Replace(ServiceDescriptor.Singleton()); + var implementationInstance = new DiagnosticContext(null); + options.Services.AddSingleton(implementationInstance); + options.Services.AddSingleton((IDiagnosticContext) implementationInstance); + }); - return Task.CompletedTask; + await _abpApplication.InitializeAsync(); + + var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService(); + + await helloWorldService.SayHelloAsync(); } - public Task StopAsync(CancellationToken cancellationToken) + public async Task StopAsync(CancellationToken cancellationToken) { - _application.Shutdown(); - - return Task.CompletedTask; + await _abpApplication.ShutdownAsync(); } } diff --git a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs index 15ff448d74..c7045a62ea 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs @@ -1,5 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Volo.Abp; using Volo.Abp.Autofac; using Volo.Abp.Modularity; @@ -10,11 +14,15 @@ namespace MyCompanyName.MyProjectName; )] public class MyProjectNameModule : AbpModule { - public override void ConfigureServices(ServiceConfigurationContext context) + public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { - var configuration = context.Services.GetConfiguration(); - var hostEnvironment = context.Services.GetSingletonInstance(); + var logger = context.ServiceProvider.GetRequiredService>(); + var configuration = context.ServiceProvider.GetRequiredService(); + logger.LogInformation($"MySettingName => {configuration["MySettingName"]}"); - context.Services.AddHostedService(); + var hostEnvironment = context.ServiceProvider.GetRequiredService(); + logger.LogInformation($"EnvironmentName => {hostEnvironment.EnvironmentName}"); + + return Task.CompletedTask; } } diff --git a/templates/console/src/MyCompanyName.MyProjectName/Program.cs b/templates/console/src/MyCompanyName.MyProjectName/Program.cs index 1ec4de497c..c6317e635d 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/Program.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/Program.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; @@ -10,7 +9,7 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static async Task Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -27,7 +26,14 @@ public class Program try { Log.Information("Starting console host."); - await CreateHostBuilder(args).RunConsoleAsync(); + + await Host.CreateDefaultBuilder(args) + .ConfigureServices(services => + { + services.AddHostedService(); + }) + .RunConsoleAsync(); + return 0; } catch (Exception ex) @@ -39,19 +45,5 @@ public class Program { Log.CloseAndFlush(); } - } - - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .UseAutofac() - .UseSerilog() - .ConfigureAppConfiguration((context, config) => - { - //setup your additional configuration sources - }) - .ConfigureServices((hostContext, services) => - { - services.AddApplication(); - }); } diff --git a/templates/console/src/MyCompanyName.MyProjectName/appsettings.json b/templates/console/src/MyCompanyName.MyProjectName/appsettings.json index 0db3279e44..1bc4c59014 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/appsettings.json +++ b/templates/console/src/MyCompanyName.MyProjectName/appsettings.json @@ -1,3 +1,3 @@ { - + "MySettingName": "MySettingValue" } diff --git a/templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs b/templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs index ab80f115c1..46ddd64910 100644 --- a/templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs +++ b/templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs @@ -1,9 +1,12 @@ using System; using System.Windows; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events; +using Serilog.Extensions.Hosting; +using Serilog.Extensions.Logging; using Volo.Abp; namespace MyCompanyName.MyProjectName; @@ -13,35 +16,40 @@ namespace MyCompanyName.MyProjectName; /// public partial class App : Application { - private readonly IHost _host; - private readonly IAbpApplicationWithExternalServiceProvider _application; + private IAbpApplicationWithInternalServiceProvider _abpApplication; - public App() + protected async override void OnStartup(StartupEventArgs e) { Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) .CreateLogger(); - _host = CreateHostBuilder(); - _application = _host.Services.GetService(); - } - - protected override async void OnStartup(StartupEventArgs e) - { try { Log.Information("Starting WPF host."); - await _host.StartAsync(); - Initialize(_host.Services); - _host.Services.GetService()?.Show(); + _abpApplication = await AbpApplicationFactory.CreateAsync(options => + { + options.UseAutofac(); + + // UseSerilog() + options.Services.AddLogging(); + options.Services.Replace(ServiceDescriptor.Singleton()); + var implementationInstance = new DiagnosticContext(null); + options.Services.AddSingleton(implementationInstance); + options.Services.AddSingleton((IDiagnosticContext) implementationInstance); + }); + + await _abpApplication.InitializeAsync(); + + _abpApplication.Services.GetRequiredService()?.Show(); } catch (Exception ex) @@ -50,28 +58,9 @@ public partial class App : Application } } - protected override async void OnExit(ExitEventArgs e) + protected async override void OnExit(ExitEventArgs e) { - _application.Shutdown(); - await _host.StopAsync(); - _host.Dispose(); + await _abpApplication.ShutdownAsync(); Log.CloseAndFlush(); } - - private void Initialize(IServiceProvider serviceProvider) - { - _application.Initialize(serviceProvider); - } - - private IHost CreateHostBuilder() - { - return Host - .CreateDefaultBuilder(null) - .UseAutofac() - .UseSerilog() - .ConfigureServices((hostContext, services) => - { - services.AddApplication(); - }).Build(); - } } diff --git a/templates/wpf/src/MyCompanyName.MyProjectName/HelloWorldService.cs b/templates/wpf/src/MyCompanyName.MyProjectName/HelloWorldService.cs index 35127360b8..2c88a265e0 100644 --- a/templates/wpf/src/MyCompanyName.MyProjectName/HelloWorldService.cs +++ b/templates/wpf/src/MyCompanyName.MyProjectName/HelloWorldService.cs @@ -1,11 +1,20 @@ -using Volo.Abp.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.DependencyInjection; namespace MyCompanyName.MyProjectName; public class HelloWorldService : ITransientDependency { + public ILogger Logger { get; set; } + + public HelloWorldService() + { + Logger = NullLogger.Instance; + } public string SayHello() { - return "\tHello world!"; + Logger.LogInformation("Call SayHello"); + return "Hello world!"; } }