diff --git a/src/System.ServiceProcess.ServiceController/tests/ServiceBaseTests.cs b/src/System.ServiceProcess.ServiceController/tests/ServiceBaseTests.cs index daf0ccc49e1b..96fd00329527 100644 --- a/src/System.ServiceProcess.ServiceController/tests/ServiceBaseTests.cs +++ b/src/System.ServiceProcess.ServiceController/tests/ServiceBaseTests.cs @@ -21,6 +21,8 @@ public class ServiceBaseTests : IDisposable private static readonly Lazy s_isElevated = new Lazy(() => AdminHelpers.IsProcessElevated()); protected static bool IsProcessElevated => s_isElevated.Value; + private bool _disposed; + public ServiceBaseTests() { _testService = new TestServiceProvider(); @@ -174,7 +176,11 @@ public void TestOnContinueBeforePause() public void Dispose() { - _testService.DeleteTestServices(); + if (!_disposed) + { + _testService.DeleteTestServices(); + _disposed = true; + } } } } diff --git a/src/System.ServiceProcess.ServiceController/tests/ServiceControllerTests.cs b/src/System.ServiceProcess.ServiceController/tests/ServiceControllerTests.cs index 2511ca5e1817..11978dfe0676 100644 --- a/src/System.ServiceProcess.ServiceController/tests/ServiceControllerTests.cs +++ b/src/System.ServiceProcess.ServiceController/tests/ServiceControllerTests.cs @@ -19,6 +19,7 @@ public class ServiceControllerTests : IDisposable protected static bool IsProcessElevated => s_isElevated.Value; private const int ExpectedDependentServiceCount = 3; + private bool _disposed; public ServiceControllerTests() { @@ -190,7 +191,11 @@ public void ServicesStartMode() public void Dispose() { - _testService.DeleteTestServices(); + if (!_disposed) + { + _testService.DeleteTestServices(); + _disposed = true; + } } private static ServiceController AssertHasDependent(ServiceController controller, string serviceName, string displayName) diff --git a/src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestServiceInstaller.cs b/src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestServiceInstaller.cs index 9bd9c128073a..878f560386a0 100644 --- a/src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestServiceInstaller.cs +++ b/src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestServiceInstaller.cs @@ -14,6 +14,8 @@ public class TestServiceInstaller { public const string LocalServiceName = "NT AUTHORITY\\LocalService"; + private string _removalStack; + public TestServiceInstaller() { } @@ -128,6 +130,19 @@ public unsafe void Install() public void RemoveService() { + if (ServiceName == null) + throw new InvalidOperationException($"Already removed service at stack ${_removalStack}"); + + // Store the stack for logging in case we're called twice + try + { + throw new Exception(); + } + catch (Exception e) + { + _removalStack = e.StackTrace; + } + // Stop the service using (ServiceController svc = new ServiceController(ServiceName)) { @@ -161,6 +176,8 @@ public void RemoveService() Interop.Advapi32.CloseServiceHandle(serviceManagerHandle); } + + ServiceName = null; } } -} +} \ No newline at end of file