Set Paramaters for Recurring Jobs

There is no way to set job parameters when Adding or Updating a recurring job. We need to set job parameters when the job is first added (not automatically queued by Hangfire’s recurring job task) so we can extract them via the JobActivator for DI. It looks like someone else has the same problem and created a fix here:

Is this something that can be added to an upcoming release?

Just wondering, why not to pass them as additional arguments to the job method?

Short answer: That’s our temporary workaround.

Long answer: We are using a global jobfilterattribute for all our background jobs which sets the appropriate job parameters. This guarantees that all background jobs will have the required job parameters for our job activator to work. It’s a very elegant and universal way of passing info used for DI. We’ve recently added some recurring jobs in addition to the standard enqueued and scheduled background jobs we already have. Unfortunately, recurring jobs don’t actually invoke the JobFilterAttribute when they are AddedOrUpdated. The JobFilterAttribute is invoked later on when Hangfire’s internal recurring jobs task creates the job (at which point the info that needs to be added to the job parameters is out of scope). Allowing job parameters to be set when a recurring job is AddedOrUpdated solves this problem. I believe the same approach is already in use for queue names on recurring jobs.

1 Like

But this is more of a workaround than a good solution, isn’t it?

We needed a thing to report errors and have different reporting depending upon the type of job, but I’d much rather be able to specify to queue a job and pass in job parameters so that the filter for running the job could then call the ErrorReporter class with a parameter than pass a parameter into the call that isn’t used for the call, just so that we can pick it up.