I have am implementing multitenancy using EF Core by referring to some guides online. I implemented the following code as a proof of concept in a console application and it works fine.
var builder = Host.CreateApplicationBuilder();
builder.Services.AddScoped<ITenantProvider, TenantProvider>();
builder.Services.AddDbContextPool<DatabaseContext>(opt =>
opt.UseNpgsql("foobar")
.UseSnakeCaseNamingConvention()
);
var app = builder.Build();
await using var serviceScope = app.Services.CreateAsyncScope();
using var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
var result = await context.Voyages.ToListAsync();
internal class TenantProvider : ITenantProvider
{
public Guid GetUserId()
=> Guid.Parse("370b98af-df6b-40a4-aa5d-25366849772f");
}
However, when moving the above code into an ASP.NET application, the same code doesn't work anymore. It throws an exception
Cannot resolve scoped service 'ITenantProvider' from root provider.
This is the minimal code of what I am doing:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<ITenantProvider, TenantProvider>();
builder.Services.AddDbContextPool<DatabaseContext>(opt =>
opt.UseNpgsql("foobar")
.UseSnakeCaseNamingConvention()
);
WebApplication app = builder.Build();
await using (var serviceScope = app.Services.CreateAsyncScope())
await using (var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>())
{
// Do something here
}
Note that the difference here is different builders (Host.CreateApplicationBuilder()
vs WebApplication.CreateBuilder()
) but for some reason it is enough for the exception. Changing the builders fixes this exception.
Does anyone have any idea on why this is happening and how to fix it?
I have am implementing multitenancy using EF Core by referring to some guides online. I implemented the following code as a proof of concept in a console application and it works fine.
var builder = Host.CreateApplicationBuilder();
builder.Services.AddScoped<ITenantProvider, TenantProvider>();
builder.Services.AddDbContextPool<DatabaseContext>(opt =>
opt.UseNpgsql("foobar")
.UseSnakeCaseNamingConvention()
);
var app = builder.Build();
await using var serviceScope = app.Services.CreateAsyncScope();
using var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
var result = await context.Voyages.ToListAsync();
internal class TenantProvider : ITenantProvider
{
public Guid GetUserId()
=> Guid.Parse("370b98af-df6b-40a4-aa5d-25366849772f");
}
However, when moving the above code into an ASP.NET application, the same code doesn't work anymore. It throws an exception
Cannot resolve scoped service 'ITenantProvider' from root provider.
This is the minimal code of what I am doing:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<ITenantProvider, TenantProvider>();
builder.Services.AddDbContextPool<DatabaseContext>(opt =>
opt.UseNpgsql("foobar")
.UseSnakeCaseNamingConvention()
);
WebApplication app = builder.Build();
await using (var serviceScope = app.Services.CreateAsyncScope())
await using (var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>())
{
// Do something here
}
Note that the difference here is different builders (Host.CreateApplicationBuilder()
vs WebApplication.CreateBuilder()
) but for some reason it is enough for the exception. Changing the builders fixes this exception.
Does anyone have any idea on why this is happening and how to fix it?
Share Improve this question edited Mar 21 at 13:49 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 21 at 11:50 GrimsonGrimson 5721 gold badge7 silver badges24 bronze badges1 Answer
Reset to default 0For future reference : I asked the same question on reddit and a nice gentleman gave me the following answer. The post is here
Pooled contexts are registered as singletons basically, which means they can't resolve scoped services.
WebApplication just enables dev only checks that throw exceptions in this case, looks like Host variant doesn't.
Changing from .AddDbContextPool()
to AddDbContext()
fixed the issue. Thank you, kind gentleman.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744356369a4570250.html
评论列表(0条)