-
Notifications
You must be signed in to change notification settings - Fork 65
/
Program.cs
90 lines (75 loc) · 2.58 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Application.DependencyInjection.Extensions;
using Infrastructure.EventStore.Contexts;
using Infrastructure.EventStore.DependencyInjection.Extensions;
using Infrastructure.EventStore.DependencyInjection.Options;
using Infrastructure.MessageBus.DependencyInjection.Extensions;
using Infrastructure.MessageBus.DependencyInjection.Options;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Quartz;
using Serilog;
var builder = Host.CreateDefaultBuilder(args);
builder.UseDefaultServiceProvider((context, provider) =>
{
provider.ValidateScopes =
provider.ValidateOnBuild =
context.HostingEnvironment.IsDevelopment();
});
builder.ConfigureAppConfiguration(configuration =>
{
configuration
.AddUserSecrets<Program>()
.AddEnvironmentVariables();
});
builder.ConfigureLogging((context, logging) =>
{
Log.Logger = new LoggerConfiguration().ReadFrom
.Configuration(context.Configuration)
.CreateLogger();
logging.ClearProviders();
logging.AddSerilog();
});
builder.ConfigureServices((context, services) =>
{
services.AddEventStore();
services.AddMessageBus();
services.AddEventBusGateway();
services.AddApplicationServices();
services.AddCommandInteractors();
services.AddEventInteractors();
services.AddMessageValidators();
services.ConfigureEventStoreOptions(
context.Configuration.GetSection(nameof(EventStoreOptions)));
services.ConfigureSqlServerRetryOptions(
context.Configuration.GetSection(nameof(SqlServerRetryOptions)));
services.ConfigureMessageBusOptions(
context.Configuration.GetSection(nameof(MessageBusOptions)));
services.ConfigureMassTransitHostOptions(
context.Configuration.GetSection(nameof(MassTransitHostOptions)));
services.ConfigureQuartzOptions(
context.Configuration.GetSection(nameof(QuartzOptions)));
});
using var host = builder.Build();
try
{
var environment = host.Services.GetRequiredService<IHostEnvironment>();
if (environment.IsDevelopment() || environment.IsStaging())
{
await using var scope = host.Services.CreateAsyncScope();
await using var dbContext = scope.ServiceProvider.GetRequiredService<EventStoreDbContext>();
await dbContext.Database.MigrateAsync();
await dbContext.Database.EnsureCreatedAsync();
}
await host.RunAsync();
Log.Information("Stopped cleanly");
}
catch (Exception ex)
{
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
await host.StopAsync();
}
finally
{
Log.CloseAndFlush();
host.Dispose();
}