C
C#2y 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
barcodeOP2y ago
I have no idea what could be causing the error, is it not being resolved or something else?
jcotton42
jcotton422y 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
barcodeOP2y 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
jcotton422y 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
barcodeOP2y ago
so a single request holds only 1 scoped object? and resets it afterwards?
jcotton42
jcotton422y 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
barcodeOP2y 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
jcotton422y ago
no you'd probably want it to be scoped what kind of app is this?
barcode
barcodeOP2y ago
CRUD asp.net postgres
jcotton42
jcotton422y ago
yeah I'd make it scoped hydrate it from the http context or whatever
barcode
barcodeOP2y ago
ty
jcotton42
jcotton422y ago
also, why is your DbContext interfaced?
barcode
barcodeOP2y ago
I hold configurations migrations and db context in another project like this
barcode
barcodeOP2y ago
barcode
barcodeOP2y ago
using this as reference point
barcode
barcodeOP2y 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
jcotton422y ago
oh, Clean Arch oh
barcode
barcodeOP2y 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
barcodeOP2y ago
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
barcode
barcodeOP2y ago
this is the latest code I tried I call AddInfrastructureServices but it seems like it doesn't attach
phaseshift
phaseshift2y ago
I guess ident middleware is taken from root scope - you cant inject mediator there
barcode
barcodeOP2y ago
mediator injection works, problem is the identity service is there some example on how to handle authentication when working with mediator
phaseshift
phaseshift2y ago
I bet there is via search
barcode
barcodeOP2y ago
i removed everything and still have the same issue are there any special nugets needed for DI?
phaseshift
phaseshift2y ago
No
barcode
barcodeOP2y ago
it resolved when I changed scoped to transient.... that's the only difference I spotted in my and example code
phaseshift
phaseshift2y ago
Giving commentary doesn't make it easy for anyone to help
barcode
barcodeOP2y ago
idk what's wrong i will ask when i figure it out
Want results from more Discord servers?
Add your server