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. 25
      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.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)

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

@ -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