/
Program.cs
94 lines (83 loc) · 3.31 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
91
92
93
94
using System.Diagnostics;
using MassTransit;
using MassTransit.Metadata;
using Microsoft.EntityFrameworkCore;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Sample.Components;
using Sample.Components.Consumers;
using Sample.Components.Services;
using Sample.Components.StateMachines;
using Serilog;
using Serilog.Events;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("MassTransit", LogEventLevel.Debug)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddDbContext<RegistrationDbContext>(x =>
{
var connectionString = hostContext.Configuration.GetConnectionString("Default");
x.UseNpgsql(connectionString, options =>
{
options.MinBatchSize(1);
});
});
services.AddOpenTelemetry().WithTracing(x =>
{
x.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService("service")
.AddTelemetrySdk()
.AddEnvironmentVariableDetector())
.AddSource("MassTransit")
.AddJaegerExporter(o =>
{
o.AgentHost = HostMetadataCache.IsRunningInContainer ? "jaeger" : "localhost";
o.AgentPort = 6831;
o.MaxPayloadSizeInBytes = 4096;
o.ExportProcessorType = ExportProcessorType.Batch;
o.BatchExportProcessorOptions = new BatchExportProcessorOptions<Activity>
{
MaxQueueSize = 2048,
ScheduledDelayMilliseconds = 5000,
ExporterTimeoutMilliseconds = 30000,
MaxExportBatchSize = 512,
};
});
});
services.AddScoped<IRegistrationValidationService, RegistrationValidationService>();
services.AddMassTransit(x =>
{
x.AddEntityFrameworkOutbox<RegistrationDbContext>(o =>
{
o.UsePostgres();
o.DuplicateDetectionWindow = TimeSpan.FromSeconds(30);
});
x.SetKebabCaseEndpointNameFormatter();
x.AddConsumer<NotifyRegistrationConsumer>();
x.AddConsumer<SendRegistrationEmailConsumer>();
x.AddConsumer<AddEventAttendeeConsumer>();
x.AddConsumer<ValidateRegistrationConsumer, ValidateRegistrationConsumerDefinition>();
x.AddSagaStateMachine<RegistrationStateMachine, RegistrationState, RegistrationStateDefinition>()
.EntityFrameworkRepository(r =>
{
r.ExistingDbContext<RegistrationDbContext>();
r.UsePostgres();
});
x.UsingRabbitMq((context, cfg) =>
{
cfg.ConfigureEndpoints(context);
});
});
})
.UseSerilog()
.Build();
await host.RunAsync();