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.");
        }
        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);
        }
Beispiel #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
        }