Simultaneous calls with BackgroundJob.Enqueue()

Hello,

I’m trying to implement HangFire on a dedicated web API based on Swagger (Swashbuckle) and I have an issue regarding multiple calls to Hangfire.

I want to execute a job on almost 9000 nodes and to do that I’m using a parallel loop like this:

   Parallel.For(0, 5000, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 5 }, i =>
        {
            var jobId = BackgroundJob.Enqueue(() => new winRM().Test(node[i].ToString());
        });

I have this kind of error after a few seconds:

The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

I don’t know where it come from (maybe SQL max connections) but when I try with a few nodes (2) it works like a charm.

PS: I’m new in C# development so sorry if this is a noob question:)

Thank you.

The error is due to the maxWorkers configured for Hangfire:

    var options = new BackgroundJobServerOptions { WorkerCount = Environment.ProcessorCount * 50 };
        app.UseHangfireDashboard();

Is there a way to increase this setting safely?

Increase the Pool-size of your SQL Connection string. The default is 100, but if your database can handle it, just set it to maybe 250.

I will try it thank you !