Browse Source

Merge pull request #13332 from abpframework/liangshiwei/console

Enhance Console template
pull/13746/head
maliming 4 years ago
committed by GitHub
parent
commit
bdce4b0f41
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs
  2. 23
      templates/console/src/MyCompanyName.MyProjectName/Program.cs

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

@ -1,42 +1,24 @@
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.Hosting; using Microsoft.Extensions.Hosting;
using Serilog;
using Volo.Abp; using Volo.Abp;
namespace MyCompanyName.MyProjectName; namespace MyCompanyName.MyProjectName;
public class MyProjectNameHostedService : IHostedService public class MyProjectNameHostedService : IHostedService
{ {
private IAbpApplicationWithInternalServiceProvider _abpApplication; private readonly IAbpApplicationWithExternalServiceProvider _abpApplication;
private readonly HelloWorldService _helloWorldService;
private readonly IConfiguration _configuration; public MyProjectNameHostedService(HelloWorldService helloWorldService, IAbpApplicationWithExternalServiceProvider abpApplication)
private readonly IHostEnvironment _hostEnvironment;
public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment)
{ {
_configuration = configuration; _helloWorldService = helloWorldService;
_hostEnvironment = hostEnvironment; _abpApplication = abpApplication;
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
_abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options => await _helloWorldService.SayHelloAsync();
{
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();
} }
public async Task StopAsync(CancellationToken cancellationToken) public async Task StopAsync(CancellationToken cancellationToken)

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

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

Loading…
Cancel
Save