Place jobfilter attributes on interface or class?


#1

When my background methods are defined in interfaces, should I put jobfilter attributes on the interface or the method implementation?

RecurringJob.AddOrUpdate<IMyInterface>(x => x.ExpireThirdPartyCredits(), Cron.Hourly);    

public interface IMyInterface
{
    AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] // here?
    void MyMethod();
}

public class MyClass
{
    //  or here?
    public void MyMethod()
    {
        // some operation here
    }
}

#2

I’d go with method implementation but I believe if the interface is decorated with that attribute the .Net can also read it.


#3

We recently had an issue where our AutomaticRetryAttribute values seemed to be completely missing. After some digging we realized that we were enqueuing the jobs using interfaces that existed without the attributes specified on them. I dug around in the Hangfire code a bit to try and figure out how the configuration attributes were applied and noticed that it was only checking for the attributes on the Type registered, not the instance Type that would be resolved for the job.