mirror of https://github.com/abpframework/abp.git
6 changed files with 96 additions and 19 deletions
@ -0,0 +1,39 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Volo.Abp; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
public class MyProjectNameHostedService : IHostedService |
|||
{ |
|||
private readonly IAbpApplicationWithExternalServiceProvider _application; |
|||
private readonly IServiceProvider _serviceProvider; |
|||
private readonly DemoService _demoService; |
|||
|
|||
public MyProjectNameHostedService( |
|||
IAbpApplicationWithExternalServiceProvider application, |
|||
IServiceProvider serviceProvider, |
|||
DemoService demoService) |
|||
{ |
|||
_application = application; |
|||
_serviceProvider = serviceProvider; |
|||
_demoService = demoService; |
|||
} |
|||
|
|||
public async Task StartAsync(CancellationToken cancellationToken) |
|||
{ |
|||
_application.Initialize(_serviceProvider); |
|||
|
|||
await _demoService.CreateTodoItemAsync(); |
|||
} |
|||
|
|||
public Task StopAsync(CancellationToken cancellationToken) |
|||
{ |
|||
_application.Shutdown(); |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +1,57 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Serilog; |
|||
using Serilog.Events; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
class Program |
|||
public class Program |
|||
{ |
|||
static async Task Main(string[] args) |
|||
public static async Task<int> Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DistDemoAppModule>(opts => |
|||
Log.Logger = new LoggerConfiguration() |
|||
#if DEBUG
|
|||
.MinimumLevel.Debug() |
|||
#else
|
|||
.MinimumLevel.Information() |
|||
#endif
|
|||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) |
|||
.Enrich.FromLogContext() |
|||
.WriteTo.Async(c => c.File("Logs/logs.txt")) |
|||
.WriteTo.Async(c => c.Console()) |
|||
.CreateLogger(); |
|||
|
|||
try |
|||
{ |
|||
opts.UseAutofac(); |
|||
})) |
|||
Log.Information("Starting console host."); |
|||
await CreateHostBuilder(args).RunConsoleAsync(); |
|||
return 0; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
application.Initialize(); |
|||
|
|||
var demoService = application.ServiceProvider.GetRequiredService<DemoService>(); |
|||
await demoService.CreateTodoItemAsync(); |
|||
|
|||
Console.WriteLine("Press ENTER to exit"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
Log.Fatal(ex, "Host terminated unexpectedly!"); |
|||
return 1; |
|||
} |
|||
finally |
|||
{ |
|||
Log.CloseAndFlush(); |
|||
} |
|||
|
|||
} |
|||
|
|||
internal static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.UseAutofac() |
|||
.UseSerilog() |
|||
.ConfigureAppConfiguration((context, config) => |
|||
{ |
|||
//setup your additional configuration sources
|
|||
}) |
|||
.ConfigureServices((hostContext, services) => |
|||
{ |
|||
services.AddApplication<DistDemoAppModule>(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue