title: C# Windows Service Timeout on startup
body: <p>I'm having difficulty trying to determine the cause of a timeout in a Windows Service I've created with C#.
  I've spent a considerable amount of time looking at several posts and topics on the issue but I am unsure what else to try.</p>
  
  <p><strong>What's the problem?</strong></p>
  
  <p>Sometimes on certain machines which run my windows service, it does not start successfully after the machine has been rebooted.
  I receive the common EventLog messages about the Service failing to start in a timely fashion and that it timed out after 30000 milliseconds.
  Windows Server 2003 machines seem to be the most common pattern but is not always isolated to just this OS. For example it works perfectly fine on other W2K3 machines.</p>
  
  <p>The startup failure can be quite random in that sometimes it will start, and other times it will fail so it is very difficult to reproduce the issue on demand.
  I am also using Log4Net to catch and errors and log them to a RollingFileAppender. However, when the service fails to start, no log file is ever created and no log information saved.
  It is as if my Service entry thread is blocking and not getting called.</p>
  
  <p><strong>Other Details:</strong></p>
  
  <ol>
  <li>The Windows service is written in C# and uses .Net 2.0</li>
  <li>There are no other service dependencies for my service when
  installed.</li>
  <li>The service exe is a Release build with no signing or authenticode
  signing.</li>
  <li>The OnStart method executes as quickly as possible by creating a
  Thread and starting that Thread. No other initialization takes place
  within OnStart.</li>
  <li>When the service does actually fail to start, opening the services
  list and starting it manually works every time and the service
  starts in probably less than a second.</li>
  </ol>
  
  <p>I have the following code added to my Program.cs which include the main entry point for the service.
  I hook in to an UnhandledException event on the CurrentDomain and am using log4net to log any unhandled errors
  There's also a try/catch around the ServiceBase.Run in the event it somehow bombs out so that I can log that error.</p>
  
  <pre><code>static void Main()
  {
      ServiceBase[] ServicesToRun;
      ServicesToRun = new ServiceBase[] 
      { 
          new SchedulerService() 
      };
  
      AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  
      try
      {
          ServiceBase.Run(ServicesToRun);
      }
      catch (Exception ex)
      {
          Log.Fatal("Unhandled Service Exception", ex);
      }
  }
  
  private static log4net.ILog _log = null;
  static log4net.ILog Log
  {
      get
      {
          if (_log == null)
          {
              if (!log4net.LogManager.GetRepository().Configured)
              {
                  log4net.Config.XmlConfigurator.Configure();
              }
  
              _log = log4net.LogManager.GetLogger(typeof(Program));
          }
          return _log;
      }
  }
  
  static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  {
      Exception ex = e.ExceptionObject as Exception;
      if (ex == null) ex = new Exception(e.ExceptionObject.ToString());
  
      Log.Fatal("Unhandled Service Exception", ex);
  }
  </code></pre>
  
  <p>The code in my inherited ServiceBase implementation is as follows:</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      Thread serviceThread = new Thread(new ThreadStart(BackgroundStart));
      serviceThread.IsBackground = true;
      serviceThread.Start();
  }
  
  private void BackgroundStart()
  {
      //Initialize and start worker objects to perform monitoring...
      //&lt;Snip&gt;
  }
  </code></pre>
  
  <p>My log4net implementation is using a ConsoleAppender and a RollingFileAppender where its configuration details are stored in the App.config.</p>
  
  <p>At this stage I am not sure what else to try.
  If any more details are needed let me know.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update:</strong>
  Just to update everyone, I'm going to try some of the suggestions such as logging to the EventLog directly or a file instead of Log4Net to see if that's the cause.
  I Will also try setting the generatePublisherEvidence in the app.config to false.
  I'm just waiting for an appropriate downtime to access the client's server to test these things out.</p>