diff --git a/test/DistEvents/DistDemoApp/DistDemoApp.csproj b/test/DistEvents/DistDemoApp/DistDemoApp.csproj index 4d88277e64..9695a92f04 100644 --- a/test/DistEvents/DistDemoApp/DistDemoApp.csproj +++ b/test/DistEvents/DistDemoApp/DistDemoApp.csproj @@ -4,7 +4,15 @@ Exe net5.0 - + + + + + + + + + diff --git a/test/DistEvents/DistDemoApp/DistDemoAppModule.cs b/test/DistEvents/DistDemoApp/DistDemoAppModule.cs index 14abd87b4c..f69f9c2380 100644 --- a/test/DistEvents/DistDemoApp/DistDemoAppModule.cs +++ b/test/DistEvents/DistDemoApp/DistDemoAppModule.cs @@ -15,6 +15,8 @@ namespace DistDemoApp { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.AddHostedService(); + context.Services.AddAbpDbContext(options => { options.AddDefaultRepositories(); diff --git a/test/DistEvents/DistDemoApp/MyProjectNameHostedService.cs b/test/DistEvents/DistDemoApp/MyProjectNameHostedService.cs new file mode 100644 index 0000000000..7522d9fe9d --- /dev/null +++ b/test/DistEvents/DistDemoApp/MyProjectNameHostedService.cs @@ -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; + } + } +} diff --git a/test/DistEvents/DistDemoApp/Program.cs b/test/DistEvents/DistDemoApp/Program.cs index bf60cac7e5..499d0c0aeb 100644 --- a/test/DistEvents/DistDemoApp/Program.cs +++ b/test/DistEvents/DistDemoApp/Program.cs @@ -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 Main(string[] args) { - using (var application = AbpApplicationFactory.Create(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(); - 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(); + }); } -} \ No newline at end of file +} diff --git a/test/DistEvents/DistDemoApp/TodoEventHandler.cs b/test/DistEvents/DistDemoApp/TodoEventHandler.cs index b48031aefd..e54278c459 100644 --- a/test/DistEvents/DistDemoApp/TodoEventHandler.cs +++ b/test/DistEvents/DistDemoApp/TodoEventHandler.cs @@ -32,7 +32,7 @@ namespace DistDemoApp if (todoSummary == null) { - await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime)); + todoSummary = await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime)); } else { diff --git a/test/DistEvents/DistDemoApp/TodoSummary.cs b/test/DistEvents/DistDemoApp/TodoSummary.cs index b0e3b395c2..70eaee1847 100644 --- a/test/DistEvents/DistDemoApp/TodoSummary.cs +++ b/test/DistEvents/DistDemoApp/TodoSummary.cs @@ -35,7 +35,7 @@ namespace DistDemoApp public override string ToString() { - return $"{base.ToString()}, {Year}-{Month:2}-{Day:2}: {TotalCount}"; + return $"{base.ToString()}, {Year}-{Month:00}-{Day:00}: {TotalCount}"; } } } \ No newline at end of file