Browse Source

Merge pull request #17721 from JadynWong/jadyn/imporve-console-template

Imporve console template with `HostApplicationBuilder`
pull/17765/head
maliming 3 years ago
committed by GitHub
parent
commit
21fe85fb26
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/AbpConfigurationExtensions.cs
  2. 20
      framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs
  3. 8
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs
  4. 23
      templates/console/src/MyCompanyName.MyProjectName/Program.cs
  5. 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);
}
}

20
framework/src/Volo.Abp.Core/Microsoft/Extensions/Hosting/AbpHostExtensions.cs

@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Threading;
namespace Microsoft.Extensions.Hosting;
public static class AbpHostExtensions
{
public static async Task InitializeAsync(this IHost host)
{
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);
}
}

8
templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs

@ -7,13 +7,11 @@ namespace MyCompanyName.MyProjectName;
public class MyProjectNameHostedService : IHostedService public class MyProjectNameHostedService : IHostedService
{ {
private readonly IAbpApplicationWithExternalServiceProvider _abpApplication;
private readonly HelloWorldService _helloWorldService; private readonly HelloWorldService _helloWorldService;
public MyProjectNameHostedService(HelloWorldService helloWorldService, IAbpApplicationWithExternalServiceProvider abpApplication) public MyProjectNameHostedService(HelloWorldService helloWorldService)
{ {
_helloWorldService = helloWorldService; _helloWorldService = helloWorldService;
_abpApplication = abpApplication;
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
@ -21,8 +19,8 @@ public class MyProjectNameHostedService : IHostedService
await _helloWorldService.SayHelloAsync(); await _helloWorldService.SayHelloAsync();
} }
public async Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)
{ {
await _abpApplication.ShutdownAsync(); return Task.CompletedTask;
} }
} }

23
templates/console/src/MyCompanyName.MyProjectName/Program.cs

@ -1,7 +1,9 @@
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 Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
using Volo.Abp; using Volo.Abp;
@ -28,19 +30,20 @@ public class Program
{ {
Log.Information("Starting console host."); Log.Information("Starting console host.");
var builder = Host.CreateDefaultBuilder(args); var builder = Host.CreateApplicationBuilder(args);
builder.ConfigureServices(services => builder.Configuration.AddAppSettingsSecretsJson();
{ builder.Logging.ClearProviders().AddSerilog();
services.AddHostedService<MyProjectNameHostedService>();
services.AddApplicationAsync<MyProjectNameModule>(options => builder.ConfigureContainer(builder.Services.AddAutofacServiceProviderFactory());
{
options.Services.ReplaceConfiguration(services.GetConfiguration()); builder.Services.AddHostedService<MyProjectNameHostedService>();
});
}).AddAppSettingsSecretsJson().UseSerilog().UseAutofac().UseConsoleLifetime(); await builder.Services.AddApplicationAsync<MyProjectNameModule>();
var host = builder.Build(); var host = builder.Build();
await host.Services.GetRequiredService<IAbpApplicationWithExternalServiceProvider>().InitializeAsync(host.Services);
await host.InitializeAsync();
await host.RunAsync(); 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