-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
60 lines (48 loc) · 1.61 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
using EasyNetQ;
using EfAudit;
using Microsoft.Extensions.Options;
using Server;
using Server.Controllers;
using Server.Handlers;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
//add efAudit services.
// this is an example for using multiple audit handlers
builder.Services.AddEfAudit(
builder.Configuration,
AuditTrailController.AddRecords, //first audit handler
RabbitMqEfAuditHandler.Handle // second audit handler
);
//register EF DbContext
builder.Services.AddDbContext<CatalogContext>((services, options) =>
{
// options.UseSqlite($"DataSource=catalog.db")
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
//add efAuditInterceptor
.AddAuditInterceptor(services);
});
//configure easynetq (rabbitmq client)
var rcs = builder.Configuration.GetConnectionString("rabbitMq");
var bus = RabbitHutch.CreateBus(rcs);
builder.Services.AddSingleton(bus);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
//warm up - validate options(optional)
app.Services.GetRequiredService<IOptionsMonitor<AuditInterceptorOptions>>();
app.Lifetime.ApplicationStopping.Register(bus.Dispose);
using var scope = app.Services.CreateScope();
//recreate the database
using (var ctx = scope.ServiceProvider.GetRequiredService<CatalogContext>())
{
ctx.Database.EnsureDeletedAsync().Wait();
ctx.Database.EnsureCreatedAsync().Wait();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.Run();