Пример #1
0
        /// <summary>
        ///   When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control
        ///   Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            try
            {
                _logger.InfoFormat("Stopping {0} service.", ApplicationParameters.Name);
                if (_subscription != null)
                {
                    _subscription.Dispose();
                }

                if (_container != null)
                {
                    var tasks = _container.GetAllInstances <ITask>();
                    foreach (var task in tasks.or_empty_list_if_null())
                    {
                        task.shutdown();
                    }
                }

                Bootstrap.shutdown();
                SimpleInjectorContainer.stop();

                _logger.InfoFormat("{0} service has shut down.", ApplicationParameters.Name);
            } catch (Exception ex)
            {
                _logger.ErrorFormat(
                    "{0} service had an error on {1} (with user {2}):{3}{4}",
                    ApplicationParameters.Name,
                    Environment.MachineName,
                    Environment.UserName,
                    Environment.NewLine,
                    ex);
            }
        }