title: Windows Service started and then stopped using Topshelf
body: <p>I am using Quartz.net and I am trying to get the Quartz server to start-off in a Windows Service. I have created a Windows Service Project and included the Quartz.net libraries. In my Service class I have:</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      try
      {
          Host host = HostFactory.New(x =&gt;
          {
              x.Service&lt;IQuartzServer&gt;(s =&gt;
              {
                  s.SetServiceName("quartz.server");
                  s.ConstructUsing(builder =&gt;
                  {
                      QuartzServer server = new QuartzServer();
                      server.Initialize();
                      return server;
                  });
                  s.WhenStarted(server =&gt; server.Start());
                  s.WhenPaused(server =&gt; server.Pause());
                  s.WhenContinued(server =&gt; server.Resume());
                  s.WhenStopped(server =&gt; server.Stop());
              });
  
              x.RunAsLocalService();
              //x.RunAs(@"mydomain\mysusername", "mypassword");
  
              x.SetDescription(Configuration.ServiceDescription);
              x.SetDisplayName(Configuration.ServiceDisplayName);
              x.SetServiceName(Configuration.ServiceName);
          });
  
          host.Run();
      }
      catch (Exception ex)
      {
          Log.Error(ex.Message);
          Log.Error(ex.InnerException.Message);
      }
  
  }
  </code></pre>
  
  <p>I have also created a Windows Service Installer and have successfully installed the Windows Service in Visual Studio's command prompt using:</p>
  
  <blockquote>
    <p>installutil MyWindowsService.exe</p>
  </blockquote>
  
  <p>When I view my service in the Windows service list and try to start the service - I get a message dialog box:</p>
  
  <pre><code>The MyWindowsService service on Local Computer started and the
  stopped. Some Services stop automatically if they are not in use by
  other services or programs.
  </code></pre>
  
  <p>Here is the output I have logged to the event viewer (log4net):</p>
  
  <h2>Windows Events</h2>
  
  <p>1</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:24,044 [11528] INFO 
  Common.Logging.Factory.AbstractLogger.Info(:0) - Finished Starting
  MyProject Windows Service."
  </code></pre>
  
  <p>2</p>
  
  <pre><code>Error   05/12/2012 14:52    Service1    "Service cannot be started.
  System.NullReferenceException: Object reference not set to an instance
  of an object.    at MyWindowsService.MyProject.OnStart(String[] args)
  in c:\My Projects\MyProject
  v40\CO40\MyWindowsService\MyProject.cs:line 58    at
  System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
  state)"
  </code></pre>
  
  <p>3</p>
  
  <pre><code>Error   05/12/2012 14:52    MyWindowsService.exe    "2012-12-05 14:52:24,042
  [6048] ERROR Common.Logging.Factory.AbstractLogger.Error(:0) - The
  Topshelf.HostConfigurators.WindowsServiceDescription service has not
  been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
  Culture=neutral, PublicKeyToken=null install'. "
  </code></pre>
  
  <p>4</p>
  
  <pre><code>Error   05/12/2012 14:52    MyWindowsService.exe    "2012-12-05 14:52:24,041
  [6048] FATAL Topshelf.Windows.WindowsServiceHost.Run(:0) - The
  Topshelf.HostConfigurators.WindowsServiceDescription service has not
  been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
  Culture=neutral, PublicKeyToken=null install'. "
  </code></pre>
  
  <p>5</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:24,039 [6048] INFO  Topshelf.Windows.WindowsServiceHost.Run(:0)
  - Starting up as a winservice application "
  </code></pre>
  
  <p>6</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:24,038 [6048] DEBUG Topshelf.Builders.RunBuilder.CreateHost(:0)
  - Running as a Windows service, using the service host "
  </code></pre>
  
  <p>7</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:24,027 [6048] INFO  Topshelf.OS.OsDetector.DetectOs(:0) -
  Detected the operating system: 'win' "
  </code></pre>
  
  <p>8</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:23,895 [6048] INFO 
  Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost(:0) -
  Topshelf v2.2.2.0, .NET Framework v4.0.30319.17929 "
  </code></pre>
  
  <p>9</p>
  
  <pre><code>Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
  14:52:23,829 [11528] INFO 
  Common.Logging.Factory.AbstractLogger.Info(:0) - Starting MyProject
  Windows Service.. "
  </code></pre>
  
  <p>Does anyone know how I can get this service to start without this error(s) being thrown?</p>
  
  <p>Thanks in advance.</p>