#pragma warning disable S3241 // Methods should not return values that are never used private int StopMe(bool silentFail = false) #pragma warning restore S3241 // Methods should not return values that are never used { var existingServiceUtil = new WindowsServiceUtil(ServiceName); if (!existingServiceUtil.IsInstalled) { return(FailWith("Unable to stop service: not installed", silentFail)); } if (!existingServiceUtil.IsStoppable) { return(FailWith("Service already stopped", silentFail)); } try { existingServiceUtil.Start(); return((int)CommandlineOptions.ExitCodes.Success); } catch (Exception ex) { return(FailWith("Unable to start service: " + ex.Message, silentFail)); } }
private int StartMe() { var existingServiceUtil = new WindowsServiceUtil(ServiceName); if (!existingServiceUtil.IsInstalled) { Console.WriteLine("Unable to start service: not installed"); return((int)CommandlineOptions.ExitCodes.Failure); } if (!IsStartable(existingServiceUtil)) { Console.WriteLine("Service cannot be started"); return((int)CommandlineOptions.ExitCodes.Failure); } try { existingServiceUtil.Start(); return((int)CommandlineOptions.ExitCodes.Success); } catch (Exception ex) { Console.WriteLine("Unable to start service: " + ex.Message); return((int)CommandlineOptions.ExitCodes.Failure); } }
private static int StartService(WindowsServiceUtil arg) { try { arg.Start(true); return(Success()); } catch (Exception ex) { return(Fail($"Unable to start {arg.ServiceName}: {ex.Message}")); } }
private bool RestartedService() { if (!_restartAt.HasValue || _restartAt.Value > Now) { return(false); } LogWarn($"Attempting to start: {Name}"); _util.Start(false); LogInfo($"Start signal sent for: {Name}"); ResetNextRestart(); return(true); }
public void BigHairyIntegrationTest() { // Arrange var serviceExe = TestServicePath; Expect(serviceExe).To.Exist($"Expected to find test service at {serviceExe}"); EnsureTestServiceIsNotInstalled(); // Act Do("Install via cli", () => Run(serviceExe, "-i", "-n", TestServiceName)); var util = new WindowsServiceUtil(TestServiceName); Do("Test is installed", () => Expect(util.IsInstalled) .To.Be.True() ); Do("Test service executable path", () => Expect(util.ServiceExe) .To.Equal(serviceExe) ); Do("Test default startup type", () => Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Automatic) ); Do("Disabled service", () => { util.Disable(); Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Disabled); }); Do("Re-enable service", () => { util.SetAutomaticStart(); Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Automatic); }); Process process = null; Do("Start service", () => { util.Start(); Expect(util.State) .To.Equal(ServiceState.Running); process = Process.GetProcesses().FirstOrDefault(p => { try { return(p?.MainModule?.FileName.Equals(serviceExe, StringComparison.OrdinalIgnoreCase) ?? false); } catch { return(false); } }); Expect(process).Not.To.Be.Null($"Service should be running: {serviceExe}"); Expect(process.Id) .To.Equal(util.ServicePID); }); var byPath = WindowsServiceUtil.GetServiceByPath(serviceExe); Expect(byPath) .Not.To.Be.Null($"Should be able to query service by path {serviceExe}"); Do("Pause service", () => { byPath.Pause(); Expect(byPath.State) .To.Equal(ServiceState.Paused); }); Do("Resume service", () => { byPath.Continue(); Expect(byPath.State) .To.Equal(ServiceState.Running); }); Do("Stop service", () => { util.Stop(); Expect(util.State) .To.Equal(ServiceState.Stopped); }); Do("Uninstall via cli", () => { Run(serviceExe, "-u", "-n", TestServiceName); Expect(util.IsInstalled) .To.Be.False(); }); Do("Install and start (api)", () => { util.InstallAndStart(); Expect(util.IsInstalled) .To.Be.True(); Expect(util.State) .To.Equal(ServiceState.Running); }); // Do("Uninstall (api)", () => util.Uninstall()); // Assert }
public void ServiceWithArgs() { // Arrange var myPath = new Uri(GetType().Assembly.Location).LocalPath; var myDir = Path.GetDirectoryName(myPath); var serviceExe = Path.Combine(myDir, "ServiceWithArgs.exe"); var logFile = Path.Combine(myDir, "service.log"); var arg1 = GetRandomString(3); var arg2 = GetRandomString(3); var args = new[] { logFile, arg1, arg2 }.Select(p => p.QuoteIfSpaced()); var serviceName = "test-service-foo-bar"; var displayName = "Test Service - Foo,Bar"; var commandline = string.Join( " ", new[] { serviceExe } .Concat(args) ); var util = new WindowsServiceUtil( serviceName, displayName, commandline ); if (util.IsInstalled) { util.Uninstall(); } if (File.Exists(logFile)) { File.Delete(logFile); } // Act using var resetter = new AutoResetter( () => util.Install(), () => util.Uninstall(true)); util.Install(); // Assert Expect(util.IsInstalled) .To.Be.True(); util.Start(); Expect(util.State) .To.Equal(ServiceState.Running); Expect(logFile) .To.Exist(); var logData = TryReadAllLinesFrom(logFile); Expect(logData) .Not.To.Be.Empty(); Expect(logData[0]) .To.Contain(arg1) .Then(arg2); util.Stop(); Expect(util.State) .To.Equal(ServiceState.Stopped); var anotherUtil = Create(serviceName); Expect(anotherUtil.Commandline) .To.Equal(commandline); Expect(anotherUtil.ServiceExe) .To.Equal(serviceExe); Expect(anotherUtil.DisplayName) .To.Equal(displayName); }
public void BigHairyIntegrationTest() { if (Platform.Is32Bit) { Assert.Ignore( "Running 32-bit: test will fail: 32-bit process cannot access 64-bit process info" ); } // Arrange var serviceExe = TestServicePath; Expect(serviceExe).To.Exist($"Expected to find test service at {serviceExe}"); EnsureTestServiceIsNotInstalled(); // Act Do("Install via cli", () => Run(serviceExe, "-i", "-n", TestServiceName)); var util = new WindowsServiceUtil(TestServiceName); Do("Test is installed", () => Expect(util.IsInstalled) .To.Be.True() ); Do("Test service commandline", () => Expect(util.Commandline) .To.Equal(serviceExe) ); Do("Test default startup type", () => Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Automatic) ); Do("Disabled service", () => { util.Disable(); Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Disabled); }); Do("Re-enable service", () => { util.SetAutomaticStart(); Expect(util.StartupType) .To.Equal(ServiceStartupTypes.Automatic); }); Do("Start service", () => { util.Start(); Expect(util.State) .To.Equal(ServiceState.Running); var processes = Process.GetProcesses(); var process = processes.FirstOrDefault(p => { try { return(p?.MainModule?.FileName.Equals(serviceExe, StringComparison.OrdinalIgnoreCase) ?? false); } catch { return(false); } }); Expect(process).Not.To.Be.Null( () => $"@Service should be running: {serviceExe}\nrunning:\n{DumpProcesses()}" ); Expect(process.Id) .To.Equal(util.ServicePID); string DumpProcesses() { return(processes.Select(p => { try { return $"{p.MainModule.FileName}"; } catch (Exception e) { return $"(unknown) ({e.Message})"; } }).OrderBy(s => s).JoinWith("\n")); ; } }); var byPath = WindowsServiceUtil.GetServiceByPath(serviceExe); Expect(byPath) .Not.To.Be.Null($"Should be able to query service by path {serviceExe}"); Do("Pause service", () => { byPath.Pause(); Expect(byPath.State) .To.Equal(ServiceState.Paused); }); Do("Resume service", () => { byPath.Continue(); Expect(byPath.State) .To.Equal(ServiceState.Running); }); Do("Stop service", () => { util.Stop(); Expect(util.State) .To.Equal(ServiceState.Stopped); }); Do("Uninstall via cli", () => { Run(serviceExe, "-u", "-n", TestServiceName); Expect(util.IsInstalled) .To.Be.False(); }); Do("Install and start (api)", () => { util = new WindowsServiceUtil( TestServiceName, TestServiceName, serviceExe); util.InstallAndStart(); Expect(util.IsInstalled) .To.Be.True(); Expect(util.State) .To.Equal(ServiceState.Running); }); Do("Re-install and start (api)", () => { util = new WindowsServiceUtil( TestServiceName, TestServiceName, serviceExe); util.Uninstall(); util.InstallAndStart(); Expect(util.IsInstalled) .To.Be.True(); Expect(util.State) .To.Equal(ServiceState.Running); }); Do("Uninstall (api)", () => util.Uninstall()); // Assert }