You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unhandled exception. System.AggregateException: Some services are not able to be constructed #119
My implementation is almost 100% like in the example except it is called ApiUser instead of ApiKey. Once I run my program I get the following:
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AspNetCore.SecurityKey.ISecurityKeyValidator Lifetime: Singleton ImplementationType: Services.DatabaseSecurityKeyValidator': Cannot consume scoped service 'Interfaces.IApiUserRepository' from singleton 'AspNetCore.SecurityKey.ISecurityKeyValidator'.) (Error while validating the service descriptor 'ServiceType: AspNetCore.SecurityKey.SecurityKeyAuthorizationFilter Lifetime: Singleton ImplementationType: AspNetCore.SecurityKey.SecurityKeyAuthorizationFilter': Cannot consume scoped service 'Interfaces.IApiUserRepository' from singleton 'AspNetCore.SecurityKey.ISecurityKeyValidator'.) (Error while validating the service descriptor 'ServiceType: AspNetCore.SecurityKey.SecurityKeyAuthenticationHandler Lifetime: Transient ImplementationType: AspNetCore.SecurityKey.SecurityKeyAuthenticationHandler': Cannot consume scoped service 'Interfaces.IApiUserRepository' from singleton 'AspNetCore.SecurityKey.ISecurityKeyValidator'.)
Am I missing something?
BTW when I use AddSecurityKey without my own validator everything works as expected.
I think the documentation is not 100% correct, passing a scoped repository will always throw that exception. Unless I did a mistake the following works and should be the correct way:
public class DatabaseSecurityKeyValidator : ISecurityKeyValidator
{
private readonly ILogger _logger;
private readonly IServiceProvider _serviceProvider;
public DatabaseSecurityKeyValidator(IServiceProvider serviceProvider, ILogger<DatabaseSecurityKeyValidator> logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}
}
using var scope = _serviceProvider.CreateScope();
var repository = scope.ServiceProvider.GetRequiredService<IApiUserRepository>();
Activity
0llirocks commentedon Jan 15, 2026
I think the documentation is not 100% correct, passing a scoped repository will always throw that exception. Unless I did a mistake the following works and should be the correct way: