/// <summary>
 /// Hard-stop the controllable service.
 /// </summary>
 /// <remarks>
 /// Use <see cref="IControllable.Shutdown"/> for normal service
 /// termination. Calling this method for a service that has already
 /// stopped has no effect.
 /// </remarks>
 public virtual void Stop()
 {
     lock (this)
     {
         if (SafeServiceState != ServiceState.Stopped)
         {
             Util.IService service = Service;
             if (service != null)
             {
                 service.Stop();
             }
             Cleanup();
             SafeServiceState = ServiceState.Stopped;
         }
     }
 }
        /// <summary>
        /// Start the <see cref="IService"/>.
        /// </summary>
        /// <param name="service">
        /// The <b>IService</b> object to start.
        /// </param>
        protected virtual void StartService(Util.IService service)
        {
            service.Configure(Config);

            if (service is IService)
            {
                IService _service = (IService)service;
                _service.UserContext = UserContext;

                if (m_memberJoined != null)
                {
                    _service.MemberJoined += new MemberEventHandler(OnMemberJoined);
                }
                if (m_memberLeaving != null)
                {
                    _service.MemberLeaving += new MemberEventHandler(OnMemberLeaving);
                }
                if (m_memberLeft != null)
                {
                    _service.MemberLeft += new MemberEventHandler(OnMemberLeft);
                }
            }

            if (m_serviceStarting != null)
            {
                service.ServiceStarting += new ServiceEventHandler(OnServiceStarting);
            }
            if (m_serviceStarted != null)
            {
                service.ServiceStarted += new ServiceEventHandler(OnServiceStarted);
            }
            if (m_serviceStopping != null)
            {
                service.ServiceStopping += new ServiceEventHandler(OnServiceStopping);
            }
            if (m_serviceStopped != null)
            {
                service.ServiceStopped += new ServiceEventHandler(OnServiceStopped);
            }

            try
            {
                service.Start();
            }
            catch (Exception e)
            {
                CacheFactory.Log("Error while starting service \""
                                 + ServiceName + "\": " + e,
                                 CacheFactory.LogLevel.Error);
                try
                {
                    service.Stop();
                }
                catch (Exception e2)
                {
                    CacheFactory.Log("Failed to stop service \""
                                     + ServiceName + "\": " + e2,
                                     CacheFactory.LogLevel.Warn);
                    // eat the exception
                }

                throw;
            }
        }