title: use hangfire as windows service by topshelf (.net core 2.2)
body: <p>I am trying to use <strong>hangfire</strong> as windows service by using <strong>Topshelf</strong> in console <strong>app .net core 2.2</strong> . I just want to <strong>load hangfire dashboard</strong>, not adding any job or anything else.</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using Topshelf;
  
  namespace HangfireAsService
  {
   class Program
  {
  static void Main(string[] args)
  {
    HostFactory.Run(config =&gt;
    {
      config.Service&lt;Bootstrap&gt;(service =&gt;
      {
        service.ConstructUsing(s =&gt; new Bootstrap());
        service.WhenStarted(s =&gt; s.Start());
        service.WhenStopped(s =&gt; s.Stop());
      });
      config.RunAsLocalSystem();
      config.SetDescription("Hangfire as windows Service for DataCrawling Project");
      config.SetDisplayName("Hangfire Service Custom");
     });
    }
   }
  }
  </code></pre>
  
  <p><strong>Bootstrap.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using Microsoft.Owin.Hosting;
  
  namespace HangfireAsService
  {
    public class Bootstrap
  {
  private IDisposable _host;
  
  
  public void Start()
  {
    var options = new StartOptions { Port = 8999 };
    _host = WebApp.Start&lt;Startup&gt;(options);
    Console.WriteLine();
    Console.WriteLine("Hangfire has started");
    Console.WriteLine("Dashboard is available at http://localhost:8999/hangfire");
    Console.WriteLine();
  }
  
  public void Stop()
  {
    _host.Dispose();
  }
  }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>using Hangfire;
  using Microsoft.AspNetCore.Builder;
  using Owin;
  using System;
  using System.Collections.Generic;
  using System.Text;
  
  
  namespace HangfireAsService
  {
   public class Startup
   {
    public void Configuration(IApplicationBuilder appBuilder)
   {
    GlobalConfiguration.Configuration
      .UseSqlServerStorage("Server=111.111.11.1\\INS2017; Database=Hangfire; user=sa; 
      password=;");
    appBuilder.UseHangfireDashboard();
    appBuilder.UseHangfireServer();
  
    }
   }
  } 
  </code></pre>
  
  <p>As you can see, I created 2 classes for my <strong>self-host owin</strong> and after reviewing <strong>the event viewer</strong> I got the error displayed below:</p>
  
  <blockquote>
    <p>The description for Event ID 0 from source HangfireAsService cannot be
    found. Either the component that raises this event is not installed on
    your local computer or the installation is corrupted. You can install
    or repair the component on the local computer.</p>
    
    <p>If the event originated on another computer, the display information
    had to be saved with the event.</p>
    
    <p>The following information was included with the event:</p>
    
    <p>Service cannot be started. System.NullReferenceException: Object
    reference not set to an instance of an object.    at
    Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()
    at
    Microsoft.Owin.Hosting.Utilities.SettingsLoader.&lt;>c.b__1_0()
    at System.Threading.LazyInitializer.EnsureInitializedCore[T](T&amp;
    target, Func<code>1 valueFactory)    at
    Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary</code>2
    settings)    at
    Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)
    at
    Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions
    options)    at
    Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions
    options)    at HangfireAsService.Bootstrap.Start() in
    C:\MyWorkSpace\Data
    Crawling\dataCrawlingConsole\HangfireAsService\Bootstrap.cs:line 17<br>
    at HangfireAsService.Program.&lt;>c.b__0_3(Bootstrap s) in
    C:\MyWorkSpace\Data
    Crawling\dataCrawlingConsole\HangfireAsService\Program.cs:line 15<br>
    at
    Topshelf.ServiceConfiguratorExtensions.&lt;>c__DisplayClass2_0<code>1.&lt;WhenStarted&gt;b__0(T
    service, HostControl control)    at
    Topshelf.Builders.DelegateServiceBuilder</code>1.DelegateServiceHandle.Start(HostControl
    hostControl)    at
    Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(String[] args)<br>
    at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
    state)</p>
  </blockquote>
  
  <p>The message resource is present, but the message was not found in the message table.</p>