Skip to content

Cannot consume scoped service IGenericProcessorFactory from singleton IOperationProcessorResolver #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jul 10, 2018 · 5 comments · Fixed by #345
Labels
bug operations json:api v1.1 operations

Comments

@ghost
Copy link

ghost commented Jul 10, 2018

Attempted 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);

@ghost ghost changed the title Operations not working with Microsoft.EntityFrameworkCore v2.0.0+ Operations not working with Microsoft.EntityFrameworkCore v2.1.0+ Jul 10, 2018
@jaredcnance
Copy link
Contributor

jaredcnance commented Jul 10, 2018

If you’re using operations, I highly recommend upgrading all the way to 2.4.0-beta1.

We are currently dogfooding this release which is why it is in beta but it has some essential fixes.
It would also be really helpful to get feedback from someone else using operations.

@ghost
Copy link
Author

ghost commented Jul 10, 2018

@jaredcnance I also tried updating to the beta release - the error persists.

Btw, I find the operations feature extremely useful, so I really do hope that this feature won't get dumped :)

@jaredcnance
Copy link
Contributor

Operations are a high priority for us and are definitely not getting dumped 😄. I don't believe the error is actually related to the version of the SqlServer provider but the lib itself. It looks like it is caused by overwriting the service registration for IGenericProcessorFactory:

services.AddSingleton<IGenericProcessorFactory, GenericProcessorFactory>();

services.AddScoped<IGenericProcessorFactory, GenericProcessorFactory>();

But really I think IOperationProcessorResolver and IGenericProcessorFactory should be scoped registrations. I'm not sure why this hasn't been caught in tests. I'll look into it further this afternoon, but you might be able to do something like this as quick fix:

services.AddJsonApi();
services.AddScoped<IOperationProcessorResolver, OperationProcessorResolver>();

@ghost
Copy link
Author

ghost commented Jul 12, 2018

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.

jaredcnance added a commit that referenced this issue Jul 19, 2018
fix(ServiceCollectionExtensions): correct service scoping
@jaredcnance jaredcnance changed the title Operations not working with Microsoft.EntityFrameworkCore v2.1.0+ Cannot consume scoped service IGenericProcessorFactory from singleton IOperationProcessorResolver Jul 19, 2018
@jaredcnance
Copy link
Contributor

This has been fixed in v2.4.0-beta2.

@jaredcnance jaredcnance added the operations json:api v1.1 operations label Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug operations json:api v1.1 operations
Development

Successfully merging a pull request may close this issue.

1 participant