diff --git a/src/DefaultBuilder/src/WebApplication.cs b/src/DefaultBuilder/src/WebApplication.cs index 9d0a837c8300..e9dcd85efc20 100644 --- a/src/DefaultBuilder/src/WebApplication.cs +++ b/src/DefaultBuilder/src/WebApplication.cs @@ -24,6 +24,8 @@ public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteB private readonly IHost _host; private readonly List _dataSources = new(); + private bool _disposed; + internal WebApplication(IHost host) { _host = host; @@ -162,12 +164,26 @@ public void Run(string? url = null) /// /// Disposes the application. /// - void IDisposable.Dispose() => _host.Dispose(); + void IDisposable.Dispose() + { + if (!_disposed) + { + _host.Dispose(); + _disposed = true; + } + } /// /// Disposes the application. /// - public ValueTask DisposeAsync() => ((IAsyncDisposable)_host).DisposeAsync(); + public async ValueTask DisposeAsync() + { + if (!_disposed) + { + await ((IAsyncDisposable)_host).DisposeAsync(); + _disposed = true; + } + } internal RequestDelegate BuildRequestDelegate() => ApplicationBuilder.Build(); RequestDelegate IApplicationBuilder.Build() => BuildRequestDelegate(); diff --git a/src/Mvc/Mvc.Testing/src/DeferredHostBuilder.cs b/src/Mvc/Mvc.Testing/src/DeferredHostBuilder.cs index ea58bf4faade..ddb3da2dd95a 100644 --- a/src/Mvc/Mvc.Testing/src/DeferredHostBuilder.cs +++ b/src/Mvc/Mvc.Testing/src/DeferredHostBuilder.cs @@ -1,10 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -124,6 +120,8 @@ private class DeferredHost : IHost, IAsyncDisposable private readonly IHost _host; private readonly TaskCompletionSource _hostStartedTcs; + private bool _disposed; + public DeferredHost(IHost host, TaskCompletionSource hostStartedTcs) { _host = host; @@ -132,13 +130,21 @@ public DeferredHost(IHost host, TaskCompletionSource hostStartedTcs) public IServiceProvider Services => _host.Services; - public void Dispose() => _host.Dispose(); + public void Dispose() + { + if (!_disposed) + { + _host.Dispose(); + _disposed = true; + } + } public async ValueTask DisposeAsync() { if (_host is IAsyncDisposable disposable) { await disposable.DisposeAsync().ConfigureAwait(false); + _disposed = true; return; } Dispose();