3

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!"));
Yaroslav
  • 358
  • 3
  • 14
  • 2
    Are you positive that `Configuration.GetConnectionString("HangfireConnection")` is always getting a value? Is it possible it's giving you `null`? – mason May 04 '21 at 20:07
  • Yes, I'm sure the connection string is set, value not null and all tables successfully created for hangfire. – Yaroslav May 04 '21 at 20:25
  • Then it sounds like you may have found a bug in Hangfire. I'd suggest you create a [mcve] and raise an issue on their GitHub. – mason May 04 '21 at 20:44
  • If you are sure about the correctness of the connection string value, sometimes dropping the Hangfire database useful. and it solved my issue. – Matt Qafouri May 05 '21 at 06:03
  • In a new project this also reproducible, the app not crash but still error logged in the console. Maybe this expected behavior, but I don't found any info about it yet, I'll try to investigate a little, and maybe create an issue on their GitHub. Also, I tried to recreate DB and change some configurations, nothing helps. – Yaroslav May 05 '21 at 19:11

1 Answers1

1

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.

Yaroslav
  • 358
  • 3
  • 14