title: How to crash to get "Subsequent failures" of Windows Service Recovery to be considered
body: <p>We have a Windows service written in C# that starts basically a Web API on a certain port. The service is configured to be restarted on the first failure and the second failure. The "Subsequent failures" is set to "Take No Action". 
  In case this port might be taken the service crashes with an unhandled exception  and in our unhandled exception callback we write dump files to a certain application directory. For whatever reason Windows keeps restarting the service over and over even though it crashed already multiple times. The structure of our service is like this: </p>
  
  
  
  <pre><code>public class WinService : ServiceBase
  {
      private WebApiHostWrapper _apiHost;
      private Thread _workerThread;
  
      public WinService()
      {
          InitializeComponent();
          ServiceName = "MyService";
          // register handler for writing dumpfiles
          AppDomain.CurrentDomain.UnhandledException += UnhandledExceptions.DomainUnhandledException; 
      }
  
      protected override void OnStart(string[] args)
      {
          _workerThread = new Thread(InternalStart) { Name = "StartupThread" };
          _workerThread.Start(args);
      }
  
      private void InternalStart(object args)
      {
          if (null == _service)
          {
              Thread.MemoryBarrier();
              _apiHost= new WebApiHostWrapper();
              _apiHost.Start((string[])args); // exception here
          }
      }
  
      protected override void OnStop()
      {
          if (null != _workerThread)
          {
              _apiHost.Dispose();
              _apiHost= null;
              if (!_workerThread.Join(5000))
              {
                  _workerThread.Abort();
              }
              Thread.MemoryBarrier();
              _workerThread = null;
          }
      }
  </code></pre>
  
  <p>In the Windows event log I see 4 entries. </p>
  
  <ul>
  <li>Service started successfully. (source: MyService)</li>
  <li>Applicatio .... stacktrace etc. (source: .NET Runtime)</li>
  <li>Faulting application name... dll and exe name (source: Application Error)</li>
  <li>Fault bucket, type 0.... (source: Windows Error Reporting)</li>
  </ul>
  
  <p>In a scenario where the port is already in use, this causes the service to crash over and over, flooding the system with dump files. Windows will always restart the service independent from the settings. Is there a special way how to crash in order to get the the "Subsequent Failures" to be considered and NOT to restart the service? </p>