Enhance Console template

pull/13332/head
liangshiwei 3 years ago
parent 2df949f1f5
commit 98c5d4d4ac

@ -1,42 +1,24 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Volo.Abp;
namespace MyCompanyName.MyProjectName;
public class MyProjectNameHostedService : IHostedService
{
private IAbpApplicationWithInternalServiceProvider _abpApplication;
private readonly IAbpApplicationWithExternalServiceProvider _abpApplication;
private readonly HelloWorldService _helloWorldService;
private readonly IConfiguration _configuration;
private readonly IHostEnvironment _hostEnvironment;
public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment)
public MyProjectNameHostedService(HelloWorldService helloWorldService, IAbpApplicationWithExternalServiceProvider abpApplication)
{
_configuration = configuration;
_hostEnvironment = hostEnvironment;
_helloWorldService = helloWorldService;
_abpApplication = abpApplication;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options =>
{
options.Services.ReplaceConfiguration(_configuration);
options.Services.AddSingleton(_hostEnvironment);
options.UseAutofac();
options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog());
});
await _abpApplication.InitializeAsync();
var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService<HelloWorldService>();
await helloWorldService.SayHelloAsync();
await _helloWorldService.SayHelloAsync();
}
public async Task StopAsync(CancellationToken cancellationToken)

@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Volo.Abp;
namespace MyCompanyName.MyProjectName;
@ -27,13 +28,23 @@ public class Program
{
Log.Information("Starting console host.");
await Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services.AddHostedService<MyProjectNameHostedService>();
services.AddApplicationAsync<MyProjectNameModule>(options =>
{
services.AddHostedService<MyProjectNameHostedService>();
})
.UseSerilog()
.RunConsoleAsync();
options.Services.ReplaceConfiguration(services.GetConfiguration());
options.UseAutofac();
options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog());
});
}).UseConsoleLifetime();
var host = builder.Build();
await host.Services.GetRequiredService<IAbpApplicationWithExternalServiceProvider>().InitializeAsync(host.Services);
await host.RunAsync();
return 0;
}
@ -47,4 +58,4 @@ public class Program
Log.CloseAndFlush();
}
}
}
}
Loading…
Cancel
Save