Browse Source

Imporve console template with `HostApplicationBuilder`

pull/17721/head
Jadyn 3 years ago
parent
commit
94763a949b
  1. 18
      framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs
  2. 8
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs
  3. 32
      templates/console/src/MyCompanyName.MyProjectName/Program.cs
  4. 10
      templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json

18
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);
}
}

8
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;
}
}

32
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<MyProjectNameHostedService>();
services.AddApplicationAsync<MyProjectNameModule>(options =>
{
options.Services.ReplaceConfiguration(services.GetConfiguration());
});
}).AddAppSettingsSecretsJson().UseSerilog().UseAutofac().UseConsoleLifetime();
builder.Configuration.AddAppSettingsSecretsJson();
builder.Logging.ClearProviders().AddSerilog();
builder.Services.AddAutofacServiceProviderFactory();
builder.Services.AddSingleton<IHostLifetime, ConsoleLifetime>();
builder.Services.AddHostedService<MyProjectNameHostedService>();
await builder.Services.AddApplicationAsync<MyProjectNameModule>();
var host = builder.Build();
await host.Services.GetRequiredService<IAbpApplicationWithExternalServiceProvider>().InitializeAsync(host.Services);
var application = host.Services.GetRequiredService<IAbpApplicationWithExternalServiceProvider>();
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(() => AsyncHelper.RunSync(() => application.ShutdownAsync()));
applicationLifetime.ApplicationStopped.Register(() => application.Dispose());
await application.InitializeAsync(host.Services);
await host.RunAsync();

10
templates/console/src/MyCompanyName.MyProjectName/Properties/launchSettings.json

@ -0,0 +1,10 @@
{
"profiles": {
"MyCompanyName.MyProjectName": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
Loading…
Cancel
Save