We are leaving Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

BackgroundJob.Enqueue to different SqlServerStorage configurations

I have a windows service running that monitors folders. When files appear, based on the which folder received the file, it schedules the job on the appropriate SQL Server.

var hangfireConnection = "GetConnectionBasedOnFolderTriggeringEvent";
JobStorage.Current = new SqlServer.SqlServerStorage( hangfireConnection );

... settings for job ...

	() => new JobInvoker().Invoke(
			Hf.JobCancellationToken.Null )

The problem seems to be that BackgroundJob has a static reference to the first SqlServerStorage setting used. I’m not sure of this, but glancing at BackgroundJob in github, it uses a static ClientFactory expression to do the actual Enqueue() method, so thinking this is set and never changed.

If I need separate Hangfire ‘environments’ (SQL Server Databases), what is proper way to pull this off?

You could try IBackgroundJobClient and use something like named services (https://autofac.readthedocs.io/en/latest/advanced/keyed-services.html) as part of dependency injection. You would configure a BackgroundJobClient for each connection string. I haven’t tried this so I can’t say for sure.

A more involved option would be to have each application expose an API that queues the job. Then have your file listener call the appropriate API.