-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
75 lines (61 loc) · 2.75 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
using Fusonic.Extensions.AspNetCore.Http;
using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Common.Transactions;
using Fusonic.Extensions.Hangfire;
using Hangfire;
using Hangfire.SqlServer;
using HangfireOutbox.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using SimpleInjector;
using SimpleInjector.Lifestyles;
var builder = WebApplication.CreateBuilder(args);
var container = new Container() { Options = { DefaultScopedLifestyle = new AsyncScopedLifestyle(), DefaultLifestyle = Lifestyle.Scoped } };
var services = builder.Services;
services.AddControllers();
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "Hangfire Outbox", Version = "v1" }));
var dataSource = builder.Configuration.GetConnectionString("app");
services.AddHangfire(x =>
{
x.UseSerializerSettings(new Newtonsoft.Json.JsonSerializerSettings() { TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All });
x.UseActivator(new ContainerJobActivator(container));
x.UseSqlServerStorage(() => new Microsoft.Data.SqlClient.SqlConnection(dataSource),
new SqlServerStorageOptions() { SchemaName = "hgf" });
});
services.AddHangfireServer();
services.AddDbContext<UserContext>(options => options.UseSqlServer(dataSource));
services.AddSimpleInjector(container, options =>
{
options.AddAspNetCore()
.AddControllerActivation();
});
container.Register<IUserAccessor, HttpContextUserAccessor>(Lifestyle.Singleton);
container.RegisterOutOfBandDecorators();
container.RegisterSingleton<IMediator, SimpleInjectorMediator>();
container.Register(typeof(IRequestHandler<,>), typeof(Program).Assembly);
container.Collection.Register(typeof(INotificationHandler<>), typeof(Program).Assembly);
container.RegisterSingleton<ITransactionScopeHandler, TransactionScopeHandler>();
container.RegisterDecorator(typeof(IRequestHandler<,>), typeof(TransactionalRequestHandlerDecorator<,>));
container.RegisterDecorator(typeof(INotificationHandler<>), typeof(TransactionalNotificationHandlerDecorator<>));
var app = builder.Build();
MigrateContext(app);
(app as IApplicationBuilder).UseSimpleInjector(container);
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "HangfireOutbox v1"));
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.UseHangfireDashboard(options: new DashboardOptions { DisplayNameFunc = DashboardHelpers.FormatJobDisplayName });
app.Run();
static void MigrateContext(WebApplication app)
{
var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<UserContext>();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}