Dashboard showing an exception on Job

I am running a recurring job every hour. My application is a Windows Service. The job seems to run correctly but the Dashboard is showing errors. On the Recurring Jobs screen I see an “Could not load assembly” exception under the Job column and “Could not find the target method” under the Processing Jobs screen.

I am executing the Recurring Job like this…

BackgroundJobServerOptions options = new BackgroundJobServerOptions { ShutdownTimeout = TimeSpan.FromMinutes(1) };
_server = new BackgroundJobServer(options);
RecurringJob.AddOrUpdate(“myJobId”, () => ExecuteOcr(JobCancellationToken.Null), Cron.HourInterval(1));

ExecuteOcr is a public method within the same class.

I found the solution, based on a suggestion from another forum thread. I just added the dashboard into the same assembly as my service. This is, IMHO, the best approach when running Hangfire in a service anyways since the dashboard is only needed when the service is running.

For others wanting to do this here is all you do…

  • Add NuGet package “Microsoft.AspNet.WebApi.OwinSelfhost”
  • In your service Start method add this line…

WebApp.Start<DashboardStartup>("http://localhost:5115");

  • Lastly create a class…

      using Hangfire;
      using Microsoft.Owin;
      using Owin;
    
      [assembly: OwinStartup(typeof(Ppm.Laserfiche.OcrService.DashboardStartup))]
    
      namespace Ppm.Laserfiche.OcrService
      {
          public class DashboardStartup
          {
              public void Configuration(IAppBuilder appBuilder)
              {
                  appBuilder.UseHangfireDashboard();
              }
          }
      }
    

Its as simple as that. The other beauty of this is you always have the dashboard running in localhost so you don’t have to worry about authorization.