'System.ArgumentNullException' in Hangfire

Viewed 647

I guess here several problems, the main it's hangfire throw this exception when the application starts.

Exception thrown: 'System.ArgumentNullException' in Hangfire.SqlServer.dll.

The application still running but it's not good for sure. And the second one, I realized that in the hangfire dashboard there are registered two servers instead of only one. I only start working with hangfire, please help me figure out what is going on.

enter image description here

enter image description here

Here how I register hangfire in startup and use it.

ConfigureServices

//HangFire
  services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
    {
        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
        QueuePollInterval = TimeSpan.Zero,
        UseRecommendedIsolationLevel = true,
        DisableGlobalLocks = true,
        
    }));
  services.AddHangfireServer(x => new BackgroundJobServerOptions {
      ShutdownTimeout = TimeSpan.FromHours(24),
      ServerTimeout = TimeSpan.FromHours(24)
  });

Configure

//HangFire
  app.UseHangfireServer();
  app.UseHangfireDashboard();
  
  app.UseEndpoints(endpoints => {
      endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
      endpoints.MapHangfireDashboard();
  });
BackgroundJob.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
1 Answers

The problem was in ConfigureServices I tried to register AddHangfireServer() method, but also in Configure I called app.UseHangfireServer();. This caused System.ArgumentNullException in runtime, the configurations such as connection string, was passed only to one hangfire server, but created was two. So to fix this error just remove excessive hangfire server registration (AddHangfireServer()) in ConfigureServices.

Related