diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs index 06679b3c48459e..6d495e0a5a3eab 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs @@ -148,9 +148,18 @@ public void ServiceCanStopItself() FileLogger.Log("host.Start()"); host.Start(); - FileLogger.Log("host.Stop()"); - await host.StopAsync(); - FileLogger.Log("host.Stop() complete"); + using (ServiceController selfController = new(nameof(ServiceCanStopItself))) + { + selfController.WaitForStatus(ServiceControllerStatus.Running, WindowsServiceTester.WaitForStatusTimeout); + Assert.Equal(ServiceControllerStatus.Running, selfController.Status); + + FileLogger.Log("host.Stop()"); + await host.StopAsync(); + FileLogger.Log("host.Stop() complete"); + + selfController.WaitForStatus(ServiceControllerStatus.Stopped, WindowsServiceTester.WaitForStatusTimeout); + Assert.Equal(ServiceControllerStatus.Stopped, selfController.Status); + } })) { FileLogger.DeleteLog(nameof(ServiceCanStopItself)); diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs index 895b4a87108ebc..880fae3e0465a4 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs @@ -48,7 +48,7 @@ private WindowsServiceTester(SafeServiceHandle serviceHandle, RemoteInvokeHandle { } } - public TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30); + public static TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30); public new void WaitForStatus(ServiceControllerStatus desiredStatus) => WaitForStatus(desiredStatus, WaitForStatusTimeout);