Background job is ignoring Queue attribute if i am using BackgroundJob.Enqueue()
Description I used a background job and added a queue attribute. I wrote a custom jobfilter attribute public class DisableMultipleQueuedItemsAttribute : JobFilterAttribute, IClientFilter, IServerFilter, IElectStateFilter {} to calculate a fingerprint to ensure a backgroundjob is not called multiple times.
In the Attribute I can read the job parameter and queue is null there.
Steps to Reproduce
------------------------- hangfire config start---------------------- var jobStorage = new SqlServerStorage(_configuration.GetConnectionString("MyConnectionString"));
// Add Hangfire services services.AddHangfire(configuration => { configuration.UseStorage(jobStorage); });
services.AddHangfireServer((_, backgroundJobServerOptions) => { backgroundJobServerOptions.Queues = new[] { "WinService", "default") }; backgroundJobServerOptions.WorkerCount = 5; }); ------------------------- hangfire config end----------------------
------------------------- Backgroundjob start---------------------- [Queue("WinService")] [DisableMultipleQueuedItems] public async Task SynchronizeAsync(int param1, int param2) {} ------------------------- Backgroundjob end----------------------
------------- calling the job -------------------- BackgroundJob.Enqueue<Scheduler>(x => x.SynchronizeAsync(param1, param2);
Observed Behavior when I set the debugger in my custom attribute the jobs queue is null
Expected Behavior Queue is set to "WinService" but is null. The correct hangfire process is executing the job
------------------------- hangfire config start---------------------- var jobStorage = new SqlServerStorage(_configuration.GetConnectionString("MyConnectionString"));
// Add Hangfire services services.AddHangfire(configuration => { configuration.UseStorage(jobStorage); });
services.AddHangfireServer((_, backgroundJobServerOptions) => { backgroundJobServerOptions.Queues = new[] { "webui") }; backgroundJobServerOptions.WorkerCount = 5; }); ------------------------- hangfire config end----------------------
Additional Information My setup was:
.NET 6 Hangfire 1.8.17
I see that the attributes are applied to some class method, e.g. to the actual implementation:
[Queue("WinService")]
[DisableMultipleQueuedItems]
public async Task SynchronizeAsync(int param1, int param2) {}
And I see that the background job is created based on the Scheduler class.
BackgroundJob.Enqueue<Scheduler>(x => x.SynchronizeAsync(param1, param2);
Please confirm that the attributes applied to the SynchornizeAsync method of the Scheduler class, because otherwise, for example when background job is created on some abstract class, base class or an interface, Hangfire will be unable to see the attributes.