Browse Source

Update the Console and WPF template.

pull/10928/head
maliming 4 years ago
parent
commit
dbb02fa744
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 16
      templates/console/src/MyCompanyName.MyProjectName/HelloWorldService.cs
  2. 58
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs
  3. 18
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs
  4. 26
      templates/console/src/MyCompanyName.MyProjectName/Program.cs
  5. 2
      templates/console/src/MyCompanyName.MyProjectName/appsettings.json
  6. 63
      templates/wpf/src/MyCompanyName.MyProjectName/App.xaml.cs
  7. 13
      templates/wpf/src/MyCompanyName.MyProjectName/HelloWorldService.cs

16
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<HelloWorldService> Logger { get; set; }
public HelloWorldService()
{
Logger = NullLogger<HelloWorldService>.Instance;
}
public Task SayHelloAsync()
{
Console.WriteLine("\tHello World!");
Logger.LogInformation("Hello World!");
return Task.CompletedTask;
}
}

58
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<MyProjectNameModule>(options =>
{
options.Services.ReplaceConfiguration(_configuration);
options.Services.AddSingleton(_hostEnvironment);
options.UseAutofac();
_helloWorldService.SayHello();
// UseSerilog()
options.Services.AddLogging();
options.Services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, SerilogLoggerFactory>());
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<HelloWorldService>();
await helloWorldService.SayHelloAsync();
}
public Task StopAsync(CancellationToken cancellationToken)
public async Task StopAsync(CancellationToken cancellationToken)
{
_application.Shutdown();
return Task.CompletedTask;
await _abpApplication.ShutdownAsync();
}
}

18
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<IHostEnvironment>();
var logger = context.ServiceProvider.GetRequiredService<ILogger<MyProjectNameModule>>();
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
logger.LogInformation($"MySettingName => {configuration["MySettingName"]}");
context.Services.AddHostedService<MyProjectNameHostedService>();
var hostEnvironment = context.ServiceProvider.GetRequiredService<IHostEnvironment>();
logger.LogInformation($"EnvironmentName => {hostEnvironment.EnvironmentName}");
return Task.CompletedTask;
}
}

26
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<int> Main(string[] args)
public async static Task<int> 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<MyProjectNameHostedService>();
})
.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<MyProjectNameModule>();
});
}

2
templates/console/src/MyCompanyName.MyProjectName/appsettings.json

@ -1,3 +1,3 @@
{
"MySettingName": "MySettingValue"
}

63
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;
/// </summary>
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<IAbpApplicationWithExternalServiceProvider>();
}
protected override async void OnStartup(StartupEventArgs e)
{
try
{
Log.Information("Starting WPF host.");
await _host.StartAsync();
Initialize(_host.Services);
_host.Services.GetService<MainWindow>()?.Show();
_abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options =>
{
options.UseAutofac();
// UseSerilog()
options.Services.AddLogging();
options.Services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, SerilogLoggerFactory>());
var implementationInstance = new DiagnosticContext(null);
options.Services.AddSingleton(implementationInstance);
options.Services.AddSingleton((IDiagnosticContext) implementationInstance);
});
await _abpApplication.InitializeAsync();
_abpApplication.Services.GetRequiredService<MainWindow>()?.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<MyProjectNameModule>();
}).Build();
}
}

13
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<HelloWorldService> Logger { get; set; }
public HelloWorldService()
{
Logger = NullLogger<HelloWorldService>.Instance;
}
public string SayHello()
{
return "\tHello world!";
Logger.LogInformation("Call SayHello");
return "Hello world!";
}
}

Loading…
Cancel
Save