-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Program.cs
44 lines (33 loc) · 1.38 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
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Performance.AspNetContextPoolingWithState;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// First, register a pooling context factory as a Singleton service, as usual:
#region RegisterSingletonContextFactory
builder.Services.AddPooledDbContextFactory<WeatherForecastContext>(
o => o.UseSqlServer(builder.Configuration.GetConnectionString("WeatherForecastContext")));
#endregion
// Register an additional context factory as a Scoped service, which gets a pooled context from the Singleton factory we registered above,
// finds the tenant ID in the web request's `HttpContext`, and injects the ID into the context:
#region RegisterScopedContextFactory
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<WeatherForecastScopedFactory>();
#endregion
// Finally, arrange for a context to get injected from our Scoped factory:
#region RegisterDbContext
builder.Services.AddScoped(
sp => sp.GetRequiredService<WeatherForecastScopedFactory>().CreateDbContext());
#endregion
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();