[Solved] Error occurred during execution of Worker # > 20

I noticed that I was getting the following error when my application attempted to poll for jobs:

|ERROR|Hangfire.Server.Worker|Error occurred during execution of 'Worker #27' component. Execution will be retried (attempt 1 of 10) in 00:00:00 seconds.

I noticed that this was only happening for worker > 20, so I did a bit of debugging and found that the default max pool size for Npgsql is 20, while Hangfire’s default worker count is 30. You’ve got a couple options for solving this problem:

  • Recommended: Decrease the Hangfire worker count to some number less than 20 (one of the overloads of UseServer has a workerCount parameter)
  • Increase the max pool size via connection string (e.g. MaxPoolSize=30) - I didn’t spend too much time on this since I don’t need 30 workers, but I did get timeouts when I put it at 30.

Figured I’d share in case others run into the same issue.