c# - Scoped service cannot be consumed in WebApplication builder but works as expected in Host.CreateApplication() - Stack Overf

I have am implementing multitenancy using EF Core by referring to some guides online. I implemented the

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

For 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信