title: Windows Service not calling OnStart() after ServiceBase.Run()
body: <p>I have a windows service in C# .NET 4.0 running on Windows 10 and I'm unable to see a call to "OnStart()" in my service after calling ServiceBase.Run(). The service is a WCF Service Host, so that I can have a long running WCF service outside of IIS.</p>
  
  <p>I've installed the service on my machine using installutil, and I'm starting it from the MMC Services view. The service appears to start correctly when I run it, but when I attach the debugger (Visual Studio 2015 Community) it fails to go any further than ServiceBase.Run().</p>
  
  <p>In windows logs I see that the service starts correctly and is reported in event viewer with "Service started successfully."</p>
  
  <p>I cant for the life of me figure out why it wont call "OnStart()" even though I'm logging at that point, nothing hits the logger to say the "OnStart()" method is called.</p>
  
  <p><strong>Main Entry Point</strong></p>
  
  <pre><code>public static class Program
  {
      private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
  
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      public static void Main()
      {
          try
          {
              ServiceBase.Run(new MyService{ServiceName = $"{nameof(MyService)}"});
          }
          catch (Exception ex)
          {
              Log.Error(ex.ToString(), ex);
          }
      }
  }
  </code></pre>
  
  <p><strong>My Service</strong></p>
  
  <pre><code>public partial class MyService: ServiceBase
  {
      private static readonly ILog Log = LogManager.GetLogger(typeof(MyService));
  
      private ServiceHost _wcfServiceHost;
  
      public MyService()
      {
          InitializeComponent();
      }
  
      protected override void OnStart(string[] args)
      {
          Log.Info($"{nameof(MyService)} {nameof(OnStart)}.");
  
          try
          {
              _wcfServiceHost?.Close();
  
              _wcfServiceHost= new ServiceHost(new WcfService());
              _wcfServiceHost.Open();
  
              Log.Info($"{nameof(MyService)} has now started a new instance of {nameof(WcfService)}");
          }
          catch (Exception ex)
          {
              Log.Error("Service host failed to start.", ex);
          }
      }
  
      protected override void OnStop()
      {
          Log.Info($"{nameof(MyService)} {nameof(OnStop)}.");
  
          if (_wcfServiceHost!= null)
          {
              _wcfServiceHost.Close();
              _wcfServiceHost= null;
          }
      }
  }
  </code></pre>
  
  <p>Following NPhillips's comment, it appears that the logger was not correctly configured. So I wasn't seeing the output of any errors, hence attaching the debugger was too late. I've cleared the logger error and I'm now getting the error below.</p>
  
  <p>I now have the following exception being logged. </p>
  
  <pre><code>System.InvalidOperationException: In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single.  This can be configured via the ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost constructors that take a Type argument.
     at System.ServiceModel.ServiceBehaviorAttribute.ApplyInstancing(ServiceDescription description, ServiceHostBase serviceHostBase)
     at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
     at System.ServiceModel.ServiceHostBase.InitializeRuntime()
     at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  </code></pre>