Skip to content

Commit

Permalink
Update Program.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
saturn72 authored May 21, 2023
1 parent 888c64d commit f51b7bb
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Sample/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,40 @@

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.
AddEfAudit(
builder.Configuration,
AuditTrailController.AddRecords,
RabbitMqEfAuditHandler.Handle
);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

//warm up - validate options
//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();
Expand All @@ -49,8 +55,6 @@
app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

0 comments on commit f51b7bb

Please sign in to comment.