public bool Handle(HostArguments args, HandleAsWindowsService service)
        {
            string str;

            System.ServiceProcess.ServiceStartMode serviceStartMode;
            string         str1;
            ServiceAccount serviceAccount;
            string         str2;
            string         str3;
            string         str4;

            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            if (!args.CommandArgs.TryGetValue("service", out str4))
            {
                return(false);
            }
            Func <KeyValuePair <string, string>, bool> func = (KeyValuePair <string, string> command) => string.Equals(command.Value, str4, StringComparison.OrdinalIgnoreCase);

            if (func(WindowsServiceHandler.InstallCommand))
            {
                WindowsServiceConfiguration windowsServiceConfiguration = (new WindowsServiceConfiguration(this.GetServiceName(service), WindowsServiceHandler.ExePath, WindowsServiceHandler.ExeArgs(args))).DisplayName(this.Prefix(service.DisplayName)).Description(service.Description);
                args.CommandArgs.TryGetValue("startmode", out str);
                if (Enum.TryParse <System.ServiceProcess.ServiceStartMode>(str, true, out serviceStartMode))
                {
                    windowsServiceConfiguration.StartMode(serviceStartMode);
                }
                if (!args.CommandArgs.TryGetValue("account", out str1))
                {
                    args.CommandArgs.TryGetValue("username", out str2);
                    args.CommandArgs.TryGetValue("password", out str3);
                    if (!string.IsNullOrWhiteSpace(str2) && !string.IsNullOrWhiteSpace(str3))
                    {
                        windowsServiceConfiguration.RunAsUser(str2, str3);
                    }
                }
                else if (Enum.TryParse <ServiceAccount>(str1, true, out serviceAccount))
                {
                    windowsServiceConfiguration.RunAs(serviceAccount);
                }
                this._windowsServices.Install(windowsServiceConfiguration);
            }
            else if (!func(WindowsServiceHandler.UninstallCommand))
            {
                this._windowsServices.Run(this.GetServiceName(service), service.OnStartFactory);
            }
            else
            {
                this._windowsServices.Uninstall(this.GetServiceName(service));
            }
            return(true);
        }
Exemplo n.º 2
0
        public bool Handle(HostArguments args, HandleAsWindowsService service)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            if (!args.CommandArgs.TryGetValue(Command, out string action))
            {
                return(false);
            }

            bool ActionIs(KeyValuePair <string, string> command) =>
            string.Equals(command.Value, action, StringComparison.OrdinalIgnoreCase);

            if (ActionIs(InstallCommand))
            {
                var configuration = new WindowsServiceConfiguration(GetServiceName(service), ExePath, ExeArgs(args))
                                    .DisplayName(Prefix(service.DisplayName))
                                    .Description(service.Description);

                args.CommandArgs.TryGetValue(ServiceStartMode, out string startMode);

                if (Enum.TryParse(startMode, true, out ServiceStartMode serviceStartMode))
                {
                    configuration.StartMode(serviceStartMode);
                }

                if (args.CommandArgs.TryGetValue(ServiceAccountCommand, out string account))
                {
                    if (Enum.TryParse(account, true, out ServiceAccount serviceAccount))
                    {
                        configuration.RunAs(serviceAccount);
                    }
                }
                else
                {
                    args.CommandArgs.TryGetValue(ServiceAccountUsernameCommand, out string username);
                    args.CommandArgs.TryGetValue(ServiceAccountPasswordCommand, out string password);

                    if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
                    {
                        configuration.RunAsUser(username, password);
                    }
                }

                _windowsServices.Install(configuration);
            }
            else if (ActionIs(UninstallCommand))
            {
                _windowsServices.Uninstall(GetServiceName(service));
            }
            else
            {
                using (var serviceBase = new CustomService(GetServiceName(service), service.OnStartFactory))
                {
                    ServiceBase.Run(serviceBase);
                }
            }

            return(true);
        }