diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index 17a1b5c6f484..2548bdcaf1ba 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -524,6 +524,7 @@ protected virtual void Dispose(bool disposing) } _server?.Dispose(); + _host?.StopAsync().Wait(); _host?.Dispose(); } diff --git a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs index ad896391b5f9..c56f835ca701 100644 --- a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs +++ b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; @@ -65,6 +65,21 @@ public void TestingInfrastructure_GenericHost_WithWithHostBuilderHasServices() Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); } + [Fact] + public void TestingInfrastructure_GenericHost_HostShouldStopBeforeDispose() + { + // Act + using var factory = new CustomizedFactory(); + var callbackCalled = false; + + var lifetimeService = (IHostApplicationLifetime) factory.Services.GetService(typeof(IHostApplicationLifetime)); + lifetimeService.ApplicationStopped.Register(() => { callbackCalled = true; }); + factory.Dispose(); + + // Assert + Assert.True(callbackCalled); + } + private class CustomizedFactory : WebApplicationFactory where TEntryPoint : class { public bool GetTestAssembliesCalled { get; private set; } @@ -73,6 +88,7 @@ private class CustomizedFactory : WebApplicationFactory ConfigureWebHostCalled { get; private set; } = new List(); + public bool DisposeHostCalled { get; private set; } protected override void ConfigureWebHost(IWebHostBuilder builder) {