question

AndrewBatta-7798 avatar image
0 Votes"
AndrewBatta-7798 asked TimonYang-MSFT commented

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

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?

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AndrewBatta-7798
How was your Windows service shut down, was it shut down manually through Services or was it similar to killing the process?
When shutting down manually, it doesn't seem to restart automatically anyway, but when I shut it down by killing the process, it successfully restarted after the time I set (1 minute).

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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;
             }
         }
     }
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.