Hangfire - two hangfire instances or services registered within the same webapp?

24 Views Asked by At

I’m not 100% sure the title explains this question fully, so I’ll try and elaborate. Apologies if it still doesn’t make sense.

I have a .net core webapp that has HF set up within it. It uses a fairly standard hangfire setup with an SQL backend to make some fire and forget calls that do some background data work. Let’s call this ‘WebApp’. I register this in my program.cs and pass it a connection string from appsettings.json when building.

Separate to this hangfire service I have built a project designed to act as a microservice that uses hangfire for processing notifications and messages. I wanted to keep this hangfire instance and database separate from the first for a number of reasons. Let’s call this ‘Mailer’.

When my app builds I can visit the two individual hangfire dashboards on the two separate urls and they both reference separate databases in their settings at the bottom of the page.

My issue comes when I try to register ‘Mailer’ as being a service my WebApp can use to queue mail related jobs. The whole solution builds and when I try to inject a backgroundjobclient into my pages and components nothing breaks, but behind the scenes it is actually injecting the IBackgroundJobClient from the WebApp’s version of Hangfire, NOT the Mailer’s one and all jobs are queued in the WebApp HF database, which means they never run (the queues have different names and if they don’t it can’t find the relevant code in the WebApp, because it is obviously in the Mailer).

I tried separately to use DI to make the Mailer’s individual services available in the WebApp, and this also seems to work, but when the jobs end up being queued they end up being queued within the WebApp’s HF database, instead of the Mailer’s one, because it is using the connection string from the webapp's instance of HF instead of the Mailer's.

Is anyone able to advise on how I could set this up? At it’s most basic, I just need the WebApp to use a connection string for its own HF database and a separate one for the Mailer’s HF database when it is interacting with the Mailer. I've had a fair bit of experience with adding scoped services that sit in separate projects, but I just can't get my head around how I pass a separate connection string and then ensure the components know which backgroundjobclient to use. Is there a way I can pass different connection strings to each instance of HF when I am registering them in the WebApp or a way I can configure the webapp's hangfire service to ensure it uses the storage for the right hangfire instance when I want to either queue mailer stuff or process data in the background?

I have tried to look into the hangfire IbackgroundJobClientFactory, but the documentation is limited and similarly there is mention of JobStorage on their documentation, but again it's light in terms of how it can actually be utilised to achieve what I want.

0

There are 0 best solutions below