- Stash a field instead of doing an equality comparison.
Fixes #54351
private bool _disposed;
private List<object> _disposables;
- public ServiceProviderEngineScope(ServiceProvider provider)
+ public ServiceProviderEngineScope(ServiceProvider provider, bool isRootScope)
{
ResolvedServices = new Dictionary<ServiceCacheKey, object>();
RootProvider = provider;
+ IsRootScope = isRootScope;
}
internal Dictionary<ServiceCacheKey, object> ResolvedServices { get; }
// For other scopes, it protects ResolvedServices and the list of disposables
internal object Sync => ResolvedServices;
- public bool IsRootScope => this == RootProvider.Root;
+ public bool IsRootScope { get; }
internal ServiceProvider RootProvider { get; }
_createServiceAccessor = CreateServiceAccessor;
_realizedServices = new ConcurrentDictionary<Type, Func<ServiceProviderEngineScope, object>>();
- Root = new ServiceProviderEngineScope(this);
+ Root = new ServiceProviderEngineScope(this, isRootScope: true);
CallSiteFactory = new CallSiteFactory(serviceDescriptors);
// The list of built in services that aren't part of the list of service descriptors
// keep this in sync with CallSiteFactory.IsService
ThrowHelper.ThrowObjectDisposedException();
}
- return new ServiceProviderEngineScope(this);
+ return new ServiceProviderEngineScope(this, isRootScope: false);
}
private ServiceProviderEngine GetEngine()
public void DoubleDisposeWorks()
{
var provider = new ServiceProvider(new ServiceCollection(), ServiceProviderOptions.Default);
- var serviceProviderEngineScope = new ServiceProviderEngineScope(provider);
+ var serviceProviderEngineScope = new ServiceProviderEngineScope(provider, isRootScope: true);
serviceProviderEngineScope.ResolvedServices.Add(new ServiceCacheKey(typeof(IFakeService), 0), null);
serviceProviderEngineScope.Dispose();
serviceProviderEngineScope.Dispose();