Skip to content

IMethodCallTranslatorPlugin is Scoped in ServiceCharacteristics but Singleton in document #26667

Closed
dotnet/EntityFramework.ApiDocs
#111

Description

File a bug

/// The service lifetime is <see cref="ServiceLifetime.Singleton" /> and multiple registrations

The document says it is singleton.

new ServiceCharacteristics(ServiceLifetime.Scoped, multipleRegistrations: true)

As the service builder says it is scoped.

Since I was directly adding ServiceDescriptor to IServiceCollection, so I cannot catch the problem with EntityFrameworkRelationalServicesBuilder.

Include stack traces

It is hard to debug and it makes the following stack trace originally:

(System.ArgumentNullException) -----
    ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
    DbContextServices.CreateModel(Boolean designTime)
    DbContextServices.get_Model()
    <>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
    CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
    CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
    CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
    CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
    CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
    <other 92 frames…>
    IServiceProvider>.get_Instance()
    InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
    AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
    DatabaseFacade.get_Dependencies()
    DatabaseFacade.EnsureCreated()

As I read the source code I guess it was a dependency injection container problem, so I modified this in ServiceProviderCache:

var serviceProvider = services.BuildServiceProvider(validateScopes: true);

Then it becomes:

System.InvalidOperationException : Cannot consume scoped service 'Microsoft.EntityFrameworkCore.Query.ISqlExpressionFactory' from singleton 'Microsoft.EntityFrameworkCore.Query.IMethodCallTranslatorPlugin'.

This makes debugging a little bit harder. Is it possible to add some debug option to turn the option validateScopes on?

Include provider and version information

EF Core version: 6.0
Database provider: Any relational provider
Target framework: .NET 6.0
Operating system: Windows 11 22000
IDE: Visual Studio 2022 17.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

area-docsclosed-fixedThe issue has been fixed and is/will be included in the release indicated by the issue milestone.customer-reportedtype-bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions