예제 #1
0
        public void InstallAlreadyInstalledService()
        {
            string serviceName = "MyServer";

            // assume the service IS installed or the test can't work
            Assert.True(InstallerServices.IsInstalled(serviceName), "The service must be installed.");

            ServiceCommands commands = new ServiceCommands();
            bool installed = commands.Install(serviceName);

            // verify that the installation failed
            Assert.False(installed, "The service installer did not fail.");
            // verify that the service is still installed from the previous instalation
            Assert.True(InstallerServices.IsInstalled(serviceName), "The service is still not installed.");
        }
예제 #2
0
        public void InstallNotAlreadyInstalledService()
        {
            string serviceName = "MyServer";

            // assume the service is NOT installed or the test can't work
            Assert.False(InstallerServices.IsInstalled(serviceName), "The service must not be installed.");

            ServiceCommands commands = new ServiceCommands();
            bool installed = commands.Install(serviceName);

            // verify that the service has been installed
            Assert.True(installed, "The service has not been installed.");
            Assert.True(InstallerServices.IsInstalled(serviceName), "The service is still not installed.");

            // installed service should be stopped by default
            string status = commands.GetStatus(serviceName);
            Assert.Equal(ServiceControllerStatus.Stopped.ToString(), status);
        }
예제 #3
0
        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
        }
예제 #4
0
 private static void ListServices(ServiceCommands commands)
 {
     List<string> services = commands.ListServices().ToList();
     services.Sort();
     var installedServiceNames = Installation.InstallerServices.GetInstalledServices().Select((service) => service.ServiceName);
     Console.WriteLine("Configured services:");
     foreach (var service in services)
     {
         bool installed = installedServiceNames.Contains(service);
         Console.WriteLine("{0}{1}", service, installed ? " - installed" : "");
     }
 }
예제 #5
0
 private static void CheckStatus(ServiceCommands commands, string serviceName)
 {
     bool isInstalled = InstallerServices.IsInstalled(serviceName);
     if (isInstalled)
     {
         string status = commands.GetStatus(serviceName);
         Console.WriteLine("Status of service '{0}': {1}.", serviceName, status);
     }
     else
     {
         Console.WriteLine("Service '{0}' is not installed.", serviceName);
     }
 }
예제 #6
0
        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));
            }
        }
예제 #7
0
        public void UninstallNotInstalledService()
        {
            string serviceName = "MyServer";

            Assert.False(InstallerServices.IsInstalled(serviceName), "The service must not be installed.");

            ServiceCommands commands = new ServiceCommands();
            bool uninstalled = commands.Uninstall(serviceName);

            // verify that the service has NOT been tried to be uninstalled
            Assert.False(uninstalled, "The service uninstaller failed to recognize a not installed service.");
        }
예제 #8
0
        public void UninstallAlreadyInstalledService()
        {
            string serviceName = "MyServer";

            Assert.True(InstallerServices.IsInstalled(serviceName), "The service must be installed.");

            ServiceCommands commands = new ServiceCommands();
            bool uninstalled = commands.Uninstall(serviceName);

            // verify that the service has been uninstalled
            Assert.True(uninstalled, "The service has not been uninstalled.");
            Assert.False(InstallerServices.IsInstalled(serviceName), "The service is still installed.");
        }
예제 #9
0
        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.");
        }
예제 #10
0
 public DaemonNtDebugRunner(string daemonNtConfigFile, string serviceName)
 {
     ServiceName = serviceName;
     DaemonNtConfigFile = daemonNtConfigFile;
     DaemonNt = new ServiceCommands() { ConfigFile = daemonNtConfigFile };
 }