From 1bce8760808d3f0bc4b439155b701921897a43ad Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Fri, 14 Jan 2022 09:11:25 -0800 Subject: [PATCH] Don't store ApplicationServiceProvider in the service provider cache key. Fixes #27169 --- src/EFCore/Internal/ServiceProviderCache.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/EFCore/Internal/ServiceProviderCache.cs b/src/EFCore/Internal/ServiceProviderCache.cs index ca38174e49e..39f5dea18e3 100644 --- a/src/EFCore/Internal/ServiceProviderCache.cs +++ b/src/EFCore/Internal/ServiceProviderCache.cs @@ -62,16 +62,25 @@ public virtual IServiceProvider GetOrAdd(IDbContextOptions options, bool provide if (coreOptionsExtension?.ServiceProviderCachingEnabled == false) { - return BuildServiceProvider(options, _configurations).ServiceProvider; + return BuildServiceProvider(options, (_configurations, options)).ServiceProvider; } - return _configurations.GetOrAdd(options, BuildServiceProvider, _configurations).ServiceProvider; + var cacheKey = options; + var extension = options.FindExtension(); + if (extension?.ApplicationServiceProvider != null) + { + cacheKey = ((DbContextOptions)options).WithExtension(extension.WithApplicationServiceProvider(null)); + } + + return _configurations.GetOrAdd(cacheKey, BuildServiceProvider, (_configurations, options)).ServiceProvider; static (IServiceProvider ServiceProvider, IDictionary DebugInfo) BuildServiceProvider( - IDbContextOptions options, - ConcurrentDictionary DebugInfo)> - configurations) + IDbContextOptions _, + (ConcurrentDictionary DebugInfo)>, + IDbContextOptions) arguments) { + var (configurations, options) = arguments; + ValidateOptions(options); var debugInfo = new Dictionary();