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 590 times
2
Tri-Edge AI
- 320
- 2
- 14
2 Answers
3
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
- 14,769
- 3
- 30
- 96
-
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
1
On ASP.NET Core +(5, 6)... you can make this:
services.AddHangfire((serviceProvider, globalConfiguration) =>
// ...
globalConfiguration.UseFilter(new AutomaticRetryAttribute { Attempts = 0 });
// ...
});
Igor
- 3,014
- 3
- 29
- 51