3

I'm using hangfire to schedule a job that runs every minute. In the same class that i have the job to run there is a Queue that i need to acess and update it's internal values. The problem is everytime hangfire executes the job it uses a new instance of the class in which the queue is empty. I've already added the class a singleton in the startup.cs. All i need to do is keep the same queue and change some string inside the queue object. Any ideas? Thanks a lot for your time!

  • 1
    If you can share what you have done so far and any errors you are getting, it will be helpful for others to answer your question. – Dula Oct 24 '21 at 01:52

1 Answers1

1

Hangfire logic is distributed. A job can be processed on any background job server (you can have many of these) So you can not assume having a shared object instance which will be updated from anywhere. To achieve this goal you would need a shared storage (like a shared cache or database) and some kind of notification to alert all apps that the value has changed.

Note that, if you only have only one hangfire client and only one hangfire server, both running in the same application, you can achieve your goal. This can be done by storing your queue in a static variable, or by using dependency injection on a singleton scope for your queue. But I think this would severely limit your architecture and you would lose most of the point of Hangfire.

jbl
  • 15,179
  • 3
  • 34
  • 101