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.
Asked
Active
Viewed 2,257 times
3

Tri-Edge AI
- 330
- 2
- 15
2 Answers
4
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 });

jbl
- 15,179
- 3
- 34
- 101
-
Will AutomaticRetryAttributes on the methods override this behaviour, though? – Tri-Edge AI Mar 04 '21 at 11:09
-
@Tri-EdgeAI No. – Igor Mar 03 '22 at 16:09
-
@Tri-EdgeAI to achieve this goal, you need a slightly different approach where you set up a custom filter provider which adds the AutomaticRetry attribute if and only if there isn't one already present. See https://stackoverflow.com/a/70334037/1236044 – jbl Mar 04 '22 at 08:17
2
On ASP.NET Core +(5, 6)... you can make this:
services.AddHangfire((serviceProvider, globalConfiguration) =>
// ...
globalConfiguration.UseFilter(new AutomaticRetryAttribute { Attempts = 0 });
// ...
});

Igor
- 3,573
- 4
- 33
- 55