public void StartAndStopInstalledService() { string serviceName = "MyServer"; Assert.True(InstallerServices.IsInstalled(serviceName), "The service must be installed."); TimeSpan timeout = new TimeSpan(0, 0, 0, 1); // 1 sec ServiceCommands commands = new ServiceCommands(); ServiceControllerStatus runningStatus = ServiceControllerStatus.Running; ServiceControllerStatus stoppedStatus = ServiceControllerStatus.Stopped; using (ServiceController sc = new ServiceController(serviceName)) { // execute bool started = commands.Start(serviceName); // verify Assert.True(started, "The service was not started."); sc.WaitForStatus(runningStatus, timeout); Assert.Equal(runningStatus.ToString(), commands.GetStatus(serviceName)); // execute bool stopped = commands.Stop(serviceName); // verify Assert.True(stopped, "The service was not stopped."); sc.WaitForStatus(stoppedStatus, timeout); Assert.Equal(stoppedStatus.ToString(), commands.GetStatus(serviceName)); } }
public static void Main(string[] args) { #region Parse command-line arguments // TODO: if the argument parsing gets too complex better use a specialized library List<string> arguments = new List<string>(); arguments.AddRange(args); if ((arguments.Count > 0) && (arguments[0] == "DaemonNT")) { arguments.RemoveAt(0); } if ((arguments.Count < 1)) { PrintUsage(); return; } ServiceCommands commands = new ServiceCommands(); bool waitAtFinishEnabled = false; // non-mandatory option parameters while ((arguments.Count > 0) && arguments[0].StartsWith("-")) { if (arguments[0].StartsWith("--config-file=")) { commands.ConfigFile = arguments[0].Split(new[] { '=' }, 2)[1]; } else if (arguments[0] == ("-w")) { waitAtFinishEnabled = true; } arguments.RemoveAt(0); } if (arguments.Count < 1) { PrintUsage(); return; } string command = arguments[0]; string serviceName = string.Empty; if (command != "list") { if (arguments.Count == 2) { serviceName = arguments[1]; } else { PrintUsage(); return; } } #endregion #region Execute the requested command try { System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); switch (command) { case "run": commands.Run(serviceName); break; case "debug": commands.DebugStart(serviceName); break; case "install": commands.Install(serviceName); break; case "uninstall": commands.Uninstall(serviceName); break; case "start": commands.Start(serviceName); break; case "stop": commands.Stop(serviceName); break; case "restart": commands.Restart(serviceName); break; case "status": CheckStatus(commands, serviceName); break; case "list": ListServices(commands); break; default: PrintUsage(); return; } } catch (SecurityException) { Console.Error.WriteLine(string.Format( "Error: the '{0}' command requires administrator privileges.", command)); ExitWithStatus(-2, waitAtFinishEnabled); } catch (Exception ex) { Console.Error.WriteLine("Error: {0}", ex.Message); ExitWithStatus(-1, waitAtFinishEnabled); } WaitAtFinish(waitAtFinishEnabled); #endregion }
public void StartNotInstalledService() { string serviceName = "MyServer"; Assert.False(InstallerServices.IsInstalled(serviceName), "The service must not be installed."); ServiceCommands commands = new ServiceCommands(); bool started = commands.Start(serviceName); Assert.False(started, "The service was started even if it was not installed."); }