admin管理员组文章数量:1406177
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.
本文标签:
版权声明:本文标题:c# - Scoped service cannot be consumed in WebApplication builder but works as expected in Host.CreateApplication() - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744356369a2602324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论