How can I set retry count to 0 by default for all Hangfire jobs?

Viewed 917

I am using C# ASP.NET Core 3.1 with Hangfire to schedule background jobs. By default if an exception occurs during the execution of the background job, there are 10 retry attempts. I am aware it is possible to use the AutomaticRetry attribute to set that to 0, but I don't want to do that on every single background job. Instead I want the default to be 0 and to only specify it manually with the attribute in the very very rare case that I want it to be non-0. Thanks.

2 Answers

I used to do it like this in .Net Classic, I guess it should not be so different in .Net Core

// disable automatic retry if a job fails
Hangfire.GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute() { Attempts = 0 }); 

On ASP.NET Core +(5, 6)... you can make this:

services.AddHangfire((serviceProvider, globalConfiguration) =>
    
    // ...
    globalConfiguration.UseFilter(new AutomaticRetryAttribute { Attempts = 0 });
    // ...
});       
Related