Skip to content

Commit

Permalink
Store ApplicationServiceProvider in a WeakReference
Browse files Browse the repository at this point in the history
Fixes #27169
  • Loading branch information
AndriySvyryd committed Jan 14, 2022
1 parent e7fa151 commit b4b7641
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/EFCore/Infrastructure/CoreOptionsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
public class CoreOptionsExtension : IDbContextOptionsExtension
{
private IServiceProvider? _internalServiceProvider;
private IServiceProvider? _applicationServiceProvider;
private WeakReference<IServiceProvider>? _applicationServiceProvider;
private IModel? _model;
private ILoggerFactory? _loggerFactory;
private IDbContextLogger? _contextLogger;
Expand Down Expand Up @@ -72,7 +72,7 @@ public CoreOptionsExtension()
protected CoreOptionsExtension(CoreOptionsExtension copyFrom)
{
_internalServiceProvider = copyFrom.InternalServiceProvider;
_applicationServiceProvider = copyFrom.ApplicationServiceProvider;
_applicationServiceProvider = copyFrom._applicationServiceProvider;
_model = copyFrom.Model;
_loggerFactory = copyFrom.LoggerFactory;
_contextLogger = copyFrom.DbContextLogger;
Expand Down Expand Up @@ -131,7 +131,9 @@ public virtual CoreOptionsExtension WithApplicationServiceProvider(IServiceProvi
{
var clone = Clone();

clone._applicationServiceProvider = applicationServiceProvider;
clone._applicationServiceProvider = applicationServiceProvider == null
? null
: new WeakReference<IServiceProvider>(applicationServiceProvider);

return clone;
}
Expand Down Expand Up @@ -412,7 +414,10 @@ public virtual IServiceProvider? InternalServiceProvider
/// The option set from the <see cref="DbContextOptionsBuilder.UseApplicationServiceProvider" /> method.
/// </summary>
public virtual IServiceProvider? ApplicationServiceProvider
=> _applicationServiceProvider;
=> _applicationServiceProvider == null
|| !_applicationServiceProvider.TryGetTarget(out var provider)
? null
: provider;

/// <summary>
/// The options set from the <see cref="DbContextOptionsBuilder.ConfigureWarnings" /> method.
Expand Down

0 comments on commit b4b7641

Please sign in to comment.