diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs index 891110fa0a870..23073928252b7 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs @@ -53,13 +53,13 @@ internal object CaptureDisposable(object service) { if (_disposed) { - if (_disposables != null) + if (service is IDisposable disposable) { - // cleanup disposable just in case still has items before throwing - foreach (IDisposable disposable in _disposables) - { - disposable.Dispose(); - } + disposable.Dispose(); + } + else + { + ((IAsyncDisposable)service).DisposeAsync().AsTask().Wait(); } ThrowHelper.ThrowObjectDisposedException(); }