private void EnsureWindowsServiceIsInstalledAndStarted()
        {
            if (_windowsServices.Exists(WindowsServiceName))
            {
                ServiceControllerStatus serviceStatus = _windowsServices.GetStatus(WindowsServiceName);

                if (serviceStatus != ServiceControllerStatus.Running)
                {
                    _consoleWriter.WriteLine($"Starting windows service '{WindowsServiceDisplayName}' ('{WindowsServiceName}')");

                    _windowsServices.Start(WindowsServiceName);

                    serviceStatus = _windowsServices.GetStatus(WindowsServiceName);
                }

                _consoleWriter.WriteLine($"Windows service '{WindowsServiceDisplayName}' ('{WindowsServiceName}') has status {serviceStatus}.");
                return;
            }

            _consoleWriter.WriteLine($"Installing '{WindowsServiceDisplayName}' ('{WindowsServiceName}') as windows service");
            _consoleWriter.WriteLine();

            var configuration = new WindowsServiceConfiguration(WindowsServiceName, ExePath, "-service")
                                .DisplayName(WindowsServiceDisplayName)
                                .Description(Description)
                                .StartMode(ServiceStartMode.Automatic);

            _windowsServices.Install(configuration);

            try
            {
                _windowsServices.Start(WindowsServiceName);

                _consoleWriter.WriteLine($"Windows service '{WindowsServiceDisplayName}' ('{WindowsServiceName}') started.");
            }
            catch (Exception ex)
            {
                _consoleWriter.WriteLine();
                _consoleWriter.WriteLine("NOTICE: Attempted to start the windows service, but failed with: ");
                _consoleWriter.WriteLine();
                _consoleWriter.WriteLine(ex.ToString());
            }
        }
Exemple #2
0
        public IHttpActionResult GetStatus([FromUri] string serviceName = null)
        {
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                return(BadRequest($"Missing required value for querystring '{nameof(serviceName)}'."));
            }

            bool exists = _windowsServices.Exists(serviceName);

            if (!exists)
            {
                return(NotFound());
            }

            return(Ok(_windowsServices.GetStatus(serviceName)));
        }