diff --git a/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt b/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..5cebe89b2612 100644 --- a/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt +++ b/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ #nullable enable +Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.DisposeAsync() -> System.Threading.Tasks.ValueTask diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index 151c0a045a88..412f37a3a510 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -8,13 +8,13 @@ using System.Net.Http; using System.Reflection; using System.Text.Json; -using System.Runtime.Serialization.Json; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.Hosting; +using System.Threading.Tasks; namespace Microsoft.AspNetCore.Mvc.Testing { @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// /// A type in the entry point assembly of the application. /// Typically the Startup or Program classes can be used. - public class WebApplicationFactory : IDisposable where TEntryPoint : class + public class WebApplicationFactory : IDisposable, IAsyncDisposable where TEntryPoint : class { private bool _disposed; private TestServer _server; @@ -523,13 +523,21 @@ protected virtual void Dispose(bool disposing) } _server?.Dispose(); - _host?.StopAsync().Wait(); + _host?.StopAsync().GetAwaiter().GetResult(); _host?.Dispose(); } _disposed = true; } + /// + public ValueTask DisposeAsync() + { + Dispose(disposing: false); + GC.SuppressFinalize(this); + return ValueTask.CompletedTask; + } + private class DelegatedWebApplicationFactory : WebApplicationFactory { private readonly Func _createServer; diff --git a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs index c56f835ca701..0a4942123c39 100644 --- a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs +++ b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; @@ -80,6 +81,21 @@ public void TestingInfrastructure_GenericHost_HostShouldStopBeforeDispose() Assert.True(callbackCalled); } + [Fact] + public async Task TestingInfrastructure_GenericHost_HostDisposeAsync() + { + // Act + await using var factory = new CustomizedFactory(); + var callbackCalled = false; + + var lifetimeService = (IHostApplicationLifetime) factory.Services.GetService(typeof(IHostApplicationLifetime)); + lifetimeService.ApplicationStopped.Register(() => { callbackCalled = true; }); + await factory.DisposeAsync(); + + // Assert + Assert.True(callbackCalled); + } + private class CustomizedFactory : WebApplicationFactory where TEntryPoint : class { public bool GetTestAssembliesCalled { get; private set; } @@ -88,7 +104,6 @@ private class CustomizedFactory : WebApplicationFactory ConfigureWebHostCalled { get; private set; } = new List(); - public bool DisposeHostCalled { get; private set; } protected override void ConfigureWebHost(IWebHostBuilder builder) {