C
C#15mo ago
barcode

✅ Dependency Injection Exception

I'm having issues accessing dbcontext in my dependency injection project. I get an exception
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: GymRadar.Application.Contracts.Identity.IIdentityService Lifetime: Singleton ImplementationType: GymRadar.Infrastructure.Identity.IdentityService': Cannot consume scoped service 'GymRadar.Application.Contracts.Persistence.IGymRadarDbContext' from singleton 'GymRadar.Application.Contracts.Identity.IIdentityService'.)
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: GymRadar.Application.Contracts.Identity.IIdentityService Lifetime: Singleton ImplementationType: GymRadar.Infrastructure.Identity.IdentityService': Cannot consume scoped service 'GymRadar.Application.Contracts.Persistence.IGymRadarDbContext' from singleton 'GymRadar.Application.Contracts.Identity.IIdentityService'.)
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices();
builder.Services.AddPersistenceServices(builder.Configuration);
...
}
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices();
builder.Services.AddPersistenceServices(builder.Configuration);
...
}
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services)
{
services.AddSingleton<IIdentityService, IdentityService>();
services.AddTransient<IIdentityContext, IdentityContext>();
return services;
}
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services)
{
services.AddSingleton<IIdentityService, IdentityService>();
services.AddTransient<IIdentityContext, IdentityContext>();
return services;
}
Everything works fine in the application layer, problem occurs when I try to access dbcontext in identity service
private readonly IGymRadarDbContext _dbContext;
private readonly IConfiguration _configuration;

public IdentityService(IGymRadarDbContext dbContext, IConfiguration configuration)
{
_dbContext = dbContext;
_configuration = configuration;
}
private readonly IGymRadarDbContext _dbContext;
private readonly IConfiguration _configuration;

public IdentityService(IGymRadarDbContext dbContext, IConfiguration configuration)
{
_dbContext = dbContext;
_configuration = configuration;
}
28 Replies
barcode
barcode15mo ago
I have no idea what could be causing the error, is it not being resolved or something else?
jcotton42
jcotton4215mo ago
it's right there at the very end
Cannot consume scoped service 'GymRadar.Application.Contracts.Persistence.IGymRadarDbContext' from singleton 'GymRadar.Application.Contracts.Identity.IIdentityService'.)
db context is scoped, identity is singleton you cannot use scoped services from within a singleton
barcode
barcode15mo ago
I use identityservice to take a token decode it into and id and attach the user to IdentityContext I tried making it scoped but it also didn't work :I let me see if it produces a new error also why isnt db context transient? Isn't it supposed to destruct if dbcontext.save is not called? oh yes I get a new error
System.InvalidOperationException: Cannot resolve scoped service 'GymRadar.Application.Contracts.Identity.IIdentityService' from root provider.
System.InvalidOperationException: Cannot resolve scoped service 'GymRadar.Application.Contracts.Identity.IIdentityService' from root provider.
jcotton42
jcotton4215mo ago
because usually the scope will be something like a request in asp.net, or a command in a discord bot you don't want be creating a bunch of them for no reason also makes things like transactions work cross-service
barcode
barcode15mo ago
so a single request holds only 1 scoped object? and resets it afterwards?
jcotton42
jcotton4215mo ago
no I mean in asp.net, when a request comes in, it makes a new scope from the DI container and instantiates your controllers and services from that scope
barcode
barcode15mo ago
lets say I want to hold current user identity as a DI object should that be transient like I made it be? or is there a better method to attach user context when passing it to mediatr
jcotton42
jcotton4215mo ago
no you'd probably want it to be scoped what kind of app is this?
barcode
barcode15mo ago
CRUD asp.net postgres
jcotton42
jcotton4215mo ago
yeah I'd make it scoped hydrate it from the http context or whatever
barcode
barcode15mo ago
ty
jcotton42
jcotton4215mo ago
also, why is your DbContext interfaced?
barcode
barcode15mo ago
I hold configurations migrations and db context in another project like this
barcode
barcode15mo ago
barcode
barcode15mo ago
using this as reference point
barcode
barcode15mo ago
GitHub
GitHub - jasontaylordev/CleanArchitecture: Clean Architecture Solut...
Clean Architecture Solution Template for .NET 7. Contribute to jasontaylordev/CleanArchitecture development by creating an account on GitHub.
jcotton42
jcotton4215mo ago
oh, Clean Arch oh
barcode
barcode15mo ago
forcing myself not to put everything in single project to avoid 50 folders Still can't get it working, having issues with dependency injection
System.InvalidOperationException: Cannot resolve 'MediatR.IRequestHandler`1[GymRadar.Application.Identity.IdentityAttachRequest]' from root provider because it requires scoped service 'GymRadar.Application.Contracts.Identity.IIdentityService'.
System.InvalidOperationException: Cannot resolve 'MediatR.IRequestHandler`1[GymRadar.Application.Identity.IdentityAttachRequest]' from root provider because it requires scoped service 'GymRadar.Application.Contracts.Identity.IIdentityService'.
can't figure what I'm doing wrong
barcode
barcode15mo ago
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
barcode
barcode15mo ago
this is the latest code I tried I call AddInfrastructureServices but it seems like it doesn't attach
phaseshift
phaseshift15mo ago
I guess ident middleware is taken from root scope - you cant inject mediator there
barcode
barcode15mo ago
mediator injection works, problem is the identity service is there some example on how to handle authentication when working with mediator
phaseshift
phaseshift15mo ago
I bet there is via search
barcode
barcode15mo ago
i removed everything and still have the same issue are there any special nugets needed for DI?
phaseshift
phaseshift15mo ago
No
barcode
barcode15mo ago
it resolved when I changed scoped to transient.... that's the only difference I spotted in my and example code
phaseshift
phaseshift15mo ago
Giving commentary doesn't make it easy for anyone to help
barcode
barcode15mo ago
idk what's wrong i will ask when i figure it out