From e680870076c86e5ab6a3c30a79e6420a2a77c9a2 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 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/EFCore/Internal/ServiceProviderCache.cs b/src/EFCore/Internal/ServiceProviderCache.cs index ca38174e49e..39c36795133 100644 --- a/src/EFCore/Internal/ServiceProviderCache.cs +++ b/src/EFCore/Internal/ServiceProviderCache.cs @@ -62,16 +62,26 @@ 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 != null + && 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();