How to have a Worker Service (Windows Service) Auto Restart when stopped?

Andrew Batta 21 Reputation points
2021-03-23T17:51:33.883+00:00

I installed a worker service as a windows service, enabling auto restart for 3 attempts with a delay in-between, but the service does not get restarted by Windows when it stops. Is there anyway to tell Windows to actually restart it automatically?

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-03-23T19:38:29.347+00:00

    Hello,

    Try using the following taken from the following post. I personally have not tried this but looks good going with work I've done with services as per this GitHub repository explained in a Microsoft article I wrote.

    Note that the code below does not detect if the service is running but here is code to detect if the service is running.

    public class WindowsServiceController
    {
        private string serviceName;
    
        public WindowsServiceController(string serviceName)
        {
            this.serviceName = serviceName;
        }
    
        // this method will throw an exception if the service is NOT in Running status.
        public void RestartService()
        {
            using (ServiceController service = new ServiceController(serviceName))
            {
                try
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
    
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Can not restart the Windows Service {serviceName}", ex);
                }
            }
        }
    
        // this method will throw an exception if the service is NOT in Running status.
        public void StopService()
        {
            using (ServiceController service = new ServiceController(serviceName))
            {
                try
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex);
                }
            }
        }
    
        // this method will throw an exception if the service is NOT in Stopped status.
        public void StartService()
        {
            using (ServiceController service = new ServiceController(serviceName))
            {
                try
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Can not Start the Windows Service [{serviceName}]", ex);
                }
            }
        }
    
        // if service running then restart the service if the service is stopped then start it.
        // this method will not throw an exception.
        public void StartOrRestart()
        {
            if (IsRunningStatus)
                RestartService();
            else if (IsStoppedStatus)
                StartService();
        }
    
        // stop the service if it is running. if it is already stopped then do nothing.
        // this method will not throw an exception if the service is in Stopped status.
        public void StopServiceIfRunning()
        {
            using (ServiceController service = new ServiceController(serviceName))
            {
                try
                {
                    if (!IsRunningStatus)
                        return;
    
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex);
                }
            }
        }
    
        public bool IsRunningStatus => Status == ServiceControllerStatus.Running;
    
        public bool IsStoppedStatus => Status == ServiceControllerStatus.Stopped;
    
        public ServiceControllerStatus Status
        {
            get
            {
                using (ServiceController service = new ServiceController(serviceName))
                {
                    return service.Status;
                }
            }
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.