Browse Source

Update the Console and WPF template.

pull/10928/head
maliming 5 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; using Volo.Abp.DependencyInjection;
namespace MyCompanyName.MyProjectName; namespace MyCompanyName.MyProjectName;
public class HelloWorldService : ITransientDependency 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 System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Extensions.Hosting;
using Serilog.Extensions.Logging;
using Volo.Abp; using Volo.Abp;
namespace MyCompanyName.MyProjectName; namespace MyCompanyName.MyProjectName;
public class MyProjectNameHostedService : IHostedService public class MyProjectNameHostedService : IHostedService
{ {
private readonly IAbpApplicationWithExternalServiceProvider _application; private IAbpApplicationWithInternalServiceProvider _abpApplication;
private readonly IServiceProvider _serviceProvider;
private readonly HelloWorldService _helloWorldService; private readonly IConfiguration _configuration;
private readonly IHostEnvironment _hostEnvironment;
public MyProjectNameHostedService(
IAbpApplicationWithExternalServiceProvider application, public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment)
IServiceProvider serviceProvider,
HelloWorldService helloWorldService)
{ {
_application = application; _configuration = configuration;
_serviceProvider = serviceProvider; _hostEnvironment = hostEnvironment;
_helloWorldService = helloWorldService;
} }
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(); await _abpApplication.ShutdownAsync();
return Task.CompletedTask;
} }
} }

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.Hosting;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -10,11 +14,15 @@ namespace MyCompanyName.MyProjectName;
)] )]
public class MyProjectNameModule : AbpModule public class MyProjectNameModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{ {
var configuration = context.Services.GetConfiguration(); var logger = context.ServiceProvider.GetRequiredService<ILogger<MyProjectNameModule>>();
var hostEnvironment = context.Services.GetSingletonInstance<IHostEnvironment>(); 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;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Serilog; using Serilog;
@ -10,7 +9,7 @@ namespace MyCompanyName.MyProjectName;
public class Program public class Program
{ {
public static async Task<int> Main(string[] args) public async static Task<int> Main(string[] args)
{ {
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
#if DEBUG #if DEBUG
@ -27,7 +26,14 @@ public class Program
try try
{ {
Log.Information("Starting console host."); Log.Information("Starting console host.");
await CreateHostBuilder(args).RunConsoleAsync();
await Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<MyProjectNameHostedService>();
})
.RunConsoleAsync();
return 0; return 0;
} }
catch (Exception ex) catch (Exception ex)
@ -39,19 +45,5 @@ public class Program
{ {
Log.CloseAndFlush(); 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;
using System.Windows; using System.Windows;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
using Serilog.Extensions.Hosting;
using Serilog.Extensions.Logging;
using Volo.Abp; using Volo.Abp;
namespace MyCompanyName.MyProjectName; namespace MyCompanyName.MyProjectName;
@ -13,35 +16,40 @@ namespace MyCompanyName.MyProjectName;
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
private readonly IHost _host; private IAbpApplicationWithInternalServiceProvider _abpApplication;
private readonly IAbpApplicationWithExternalServiceProvider _application;
public App() protected async override void OnStartup(StartupEventArgs e)
{ {
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
#if DEBUG #if DEBUG
.MinimumLevel.Debug() .MinimumLevel.Debug()
#else #else
.MinimumLevel.Information() .MinimumLevel.Information()
#endif #endif
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Async(c => c.File("Logs/logs.txt")) .WriteTo.Async(c => c.File("Logs/logs.txt"))
.CreateLogger(); .CreateLogger();
_host = CreateHostBuilder();
_application = _host.Services.GetService<IAbpApplicationWithExternalServiceProvider>();
}
protected override async void OnStartup(StartupEventArgs e)
{
try try
{ {
Log.Information("Starting WPF host."); 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) 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 _abpApplication.ShutdownAsync();
await _host.StopAsync();
_host.Dispose();
Log.CloseAndFlush(); 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; namespace MyCompanyName.MyProjectName;
public class HelloWorldService : ITransientDependency public class HelloWorldService : ITransientDependency
{ {
public ILogger<HelloWorldService> Logger { get; set; }
public HelloWorldService()
{
Logger = NullLogger<HelloWorldService>.Instance;
}
public string SayHello() public string SayHello()
{ {
return "\tHello world!"; Logger.LogInformation("Call SayHello");
return "Hello world!";
} }
} }

Loading…
Cancel
Save