From 3931f618b96a1c18f1b481ddb49cf7b72c01793a Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Tue, 24 Nov 2020 08:36:30 -0800 Subject: [PATCH] dispose service before throw --- .../src/ServiceLookup/ServiceProviderEngineScope.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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(); }