Skip to content

Commit 69e040e

Browse files
[Fixes #29348][Mvc] Stop the host first and then dispose it! (#29404)
Stop the host before disposing the WebApplicationFactory.
1 parent dfe625f commit 69e040e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ protected virtual void Dispose(bool disposing)
523523
}
524524

525525
_server?.Dispose();
526+
_host?.StopAsync().Wait();
526527
_host?.Dispose();
527528
}
528529

src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
@@ -65,6 +65,21 @@ public void TestingInfrastructure_GenericHost_WithWithHostBuilderHasServices()
6565
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
6666
}
6767

68+
[Fact]
69+
public void TestingInfrastructure_GenericHost_HostShouldStopBeforeDispose()
70+
{
71+
// Act
72+
using var factory = new CustomizedFactory<GenericHostWebSite.Startup>();
73+
var callbackCalled = false;
74+
75+
var lifetimeService = (IHostApplicationLifetime) factory.Services.GetService(typeof(IHostApplicationLifetime));
76+
lifetimeService.ApplicationStopped.Register(() => { callbackCalled = true; });
77+
factory.Dispose();
78+
79+
// Assert
80+
Assert.True(callbackCalled);
81+
}
82+
6883
private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
6984
{
7085
public bool GetTestAssembliesCalled { get; private set; }
@@ -73,6 +88,7 @@ private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint
7388
public bool CreateServerCalled { get; private set; }
7489
public bool CreateHostCalled { get; private set; }
7590
public IList<string> ConfigureWebHostCalled { get; private set; } = new List<string>();
91+
public bool DisposeHostCalled { get; private set; }
7692

7793
protected override void ConfigureWebHost(IWebHostBuilder builder)
7894
{

0 commit comments

Comments
 (0)