Cannot consume scoped service IGenericProcessorFactory from singleton IOperationProcessorResolver
See original GitHub issueAttempted to update from version 2.2.3 to the latest stable 2.3.3
As a consequence had to update Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools from 2.0.3 to 2.1.1
After that operations stopped working with the following error message:
"errors": [
{
"title": "InvalidOperationException",
"detail": "Cannot consume scoped service 'JsonApiDotNetCore.Internal.Generics.IGenericProcessorFactory' from singleton 'JsonApiDotNetCore.Services.Operations.IOperationProcessorResolver'.",
"status": "500",
"meta": {
"stackTrace": [
"System.InvalidOperationException: Cannot consume scoped service 'JsonApiDotNetCore.Internal.Generics.IGenericProcessorFactory' from singleton 'JsonApiDotNetCore.Services.Operations.IOperationProcessorResolver'.\r",
" at Type Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitScoped(ScopedCallSite scopedCallSite, CallSiteValidatorState state)\r",
" at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r",
" at Type Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)\r",
" at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r",
" at Type Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitSingleton(SingletonCallSite singletonCallSite, CallSiteValidatorState state)\r",
" at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r",
" at Type Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)\r",
" at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r",
" at Type Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitScoped(ScopedCallSite scopedCallSite, CallSiteValidatorState state)\r",
" at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r",
" at void Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateCallSite(IServiceCallSite callSite)\r",
" at Func<ServiceProviderEngineScope, object> Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)\r",
" at TValue System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)\r",
" at object Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)\r",
" at object Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)\r",
" at object lambda_method(Closure, IServiceProvider, object[])\r",
" at object Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(?)+CreateController(ControllerContext controllerContext)\r",
" at Task Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)\r",
" at async Task Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r",
" at async Task Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()"
]
}
}
]
}
Upon investigation I found out that updating either Microsoft.EntityFrameworkCore.SqlServer or Microsoft.EntityFrameworkCore.Tools to version 2.1.0+ brakes the operations - no matter the version of JAPDNC.
The dependency to SqlServer comes from this line in my startup:
services.AddDbContext<DomainDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")), ServiceLifetime.Transient);
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Cannot consume scoped service IMongoDbContext from ...
Scoped services only exist per-request, while singleton services are created once and the instance is shared. Now only one instance of ...
Read more >Cannot Consume Scoped Service From Singleton
What's the fix? Well either the parent needs to be scoped or transient scope. Or the child needs to be a singleton itself....
Read more >ASP.NET Core Dependency Injection - Cannot consume ...
What is happening when you consume scoped service from a singleton service is known as a captive dependency. It occurs when a service...
Read more >Using scoped services inside singletons - Sam Walpole
Trying to use scoped services inside of singletons can lead to what's known as Captive Dependencies, which can cause all sorts of nasty...
Read more >Cannot consume scoped service from IHostedService
The reason being when we add AddHostedService to the application or API the Service Instance gets added as SingleTon instance. As a good...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This does indeed fix the issue.
This issue actually might have originated a long time ago, since I did this in an old service, although from what I can remember only for debugging purposes.
This has been fixed in v2.4.0-beta2.