Disable re-queueing of failed Hangfire BackgroundJob

You can either annotate the method to run in the background with the following attribute:

[AutomaticRetry(Attempts = 0)]

Or set it globally:

GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });

Solved, using [AutomaticRetry(Attempts = 0)]


Important if using DI container with an interface, you must put the attribute on the interface definition

public interface IDataUpdater
{
    [Hangfire.AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
    void UpdateData();
}

Queue the job like this

Hangfire.RecurringJob.AddOrUpdate<IDataUpdater>(updater => updater.UpdateData(), Cron.Hourly);

Test it by just throwing any old exception within your implementation. If you've done it right you'll see this in the job history under 'deleted'.

enter image description here

Tags:

C#

Hangfire