Exemplo n.º 1
0
        void StopService()
        {
            try
            {
                if (_hasCancelled == false)
                {
                    _log.InfoFormat("Stopping the {0} service", _settings.ServiceName);

                    if (!_serviceHandle.Stop(this))
                    {
                        throw new TopshelfException("The service failed to stop (returned false).");
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error("The service did not shut down gracefully", ex);
            }
            finally
            {
                _serviceHandle.Dispose();

                _log.InfoFormat("The {0} service has stopped.", _settings.ServiceName);
            }
        }
Exemplo n.º 2
0
        public TopshelfExitCode Run()
        {
            var exitCode = TopshelfExitCode.AbnormalExit;

            try
            {
                exitCode = TopshelfExitCode.StartServiceFailed;

                _log.InfoFormat("The {0} service is being started.", _settings.ServiceName);
                _serviceHandle.Start(this);
                _log.InfoFormat("The {0} service was started.", _settings.ServiceName);

                Thread.Sleep(100);

                exitCode = TopshelfExitCode.StopServiceFailed;

                _log.InfoFormat("The {0} service is being stopped.", _settings.ServiceName);
                _serviceHandle.Stop(this);
                _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName);

                exitCode = TopshelfExitCode.Ok;
            }
            catch (Exception ex)
            {
                _settings.ExceptionCallback?.Invoke(ex);

                _log.Error("The service threw an exception during testing.", ex);
            }
            finally
            {
                _serviceHandle.Dispose();
            }

            return(exitCode);
        }
Exemplo n.º 3
0
        public override void OnStop()
        {
            try
            {
                _log.Info("[Topshelf] Stopping");

                if (!_serviceHandle.Stop(this))
                {
                    throw new TopshelfException("The service did not stop successfully (return false).");
                }

                _log.Info("[Topshelf] Stopped");
            }
            catch (Exception ex)
            {
                _log.Fatal("The service did not shut down gracefully", ex);
                throw;
            }

            if (_unhandledException != null)
            {
                _log.Info("[Topshelf] Unhandled exception detected, rethrowing to cause application to restart.");
                throw new InvalidOperationException("An unhandled exception was detected", _unhandledException);
            }
        }
Exemplo n.º 4
0
        private void RestartService(ServiceListViewItem serviceListViewItem)
        {
            try
            {
                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.QueryStatus | Advapi32.ServiceAccessRights.Start | Advapi32.ServiceAccessRights.Stop))
                    {
                        Advapi32.ServiceStatusProcess status = serviceHandle.QueryServiceStatus();

                        //Stop service (throws an exception if it is stopped)
                        serviceHandle.Stop();

                        //Wait for stop
                        serviceHandle.WaitForStatus(Advapi32.ServiceCurrentState.Stopped, TimeSpan.FromSeconds(10));

                        //Start service
                        serviceHandle.Start();
                    }
                }
            }
            catch (System.TimeoutException)
            {
                MessageBox.Show(_resManager.GetString("timeout_exception_service_restart"), _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 5
0
        public TopshelfExitCode Run()
        {
            try
            {
                _log.InfoFormat("The {0} service is being started.", _settings.ServiceName);
                _serviceHandle.Start(this);
                _log.InfoFormat("The {0} service was started.", _settings.ServiceName);

                Thread.Sleep(100);

                _log.InfoFormat("The {0} service is being stopped.", _settings.ServiceName);
                _serviceHandle.Stop(this);
            }
            catch (Exception ex)
            {
                _log.Error("The service did not shut down gracefully", ex);
            }
            finally
            {
                _serviceHandle.Dispose();
                _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName);
            }

            return(TopshelfExitCode.Ok);
        }
Exemplo n.º 6
0
        public void Stop()
        {
            var running = Interlocked.Exchange(ref _IsRunning, 0);

            if (running == 1)
            {
                this._log.Info("Service Stop requested, exiting.");
                _serviceHandle.Stop(this);
            }
            Application.ExitThread();
        }
Exemplo n.º 7
0
 private void StopService(ServiceListViewItem serviceListViewItem)
 {
     try
     {
         using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
         {
             using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.Stop))
             {
                 serviceHandle.Stop();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Exemplo n.º 8
0
        private void StopService()
        {
            _logWriter.InfoFormat(Resources.StoppingService, _settings.ServiceName);

            try
            {
                if (!_serviceHandle.Stop(this))
                {
                    throw new TopshelfException(string.Format(Resources.ServiceDidntStopSuccessfully, _settings.ServiceName));
                }
            }
            catch (Exception error)
            {
                _logWriter.Fatal(string.Format(Resources.StopServiceFailed, _settings.ServiceName), error);
            }

            _logWriter.InfoFormat(Resources.ServiceStopped, _settings.ServiceName);
        }
Exemplo n.º 9
0
        protected override void OnStop()
        {
            try
            {
                _log.Info("[Topshelf] Stopping");

                _serviceHandle.Stop(this);
            }
            catch (Exception ex)
            {
                _log.Fatal("The service did not shut down gracefully", ex);
                throw;
            }
            finally
            {
                _log.Info("[Topshelf] Stopped");
            }
        }
Exemplo n.º 10
0
        protected override void OnStop()
        {
            try
            {
                _log.Info("[Topshelf] Stopping");

                if (!_serviceHandle.Stop(this))
                {
                    throw new TopshelfException("The service did not stop successfully (return false).");
                }

                _log.Info("[Topshelf] Stopped");
            }
            catch (Exception ex)
            {
                _log.Fatal("The service did not shut down gracefully", ex);
                ExitCode = (int)TopshelfExitCode.StopServiceFailed;
                throw;
            }
        }
Exemplo n.º 11
0
 public bool Stop(HostControl hostControl)
 {
     return(_serviceHandle.Stop(hostControl));
 }
Exemplo n.º 12
0
 bool ServiceHandle.Stop(HostControl hostControl)
 {
     return(_service.Stop(_hostControl));
 }
Exemplo n.º 13
0
        bool ServiceHandle.Stop(HostControl hostControl)
        {
            var control = new AppDomainHostControl(this);

            return(_service.Stop(control));
        }