Check if code runs inside a hangfire job

is there a way I can check in my code that it’s running withing a job context? This code might be deep into my application logic, and not directly in the enqueued method.


Not really. The method knows nothing about the environment it is executing in, and it actually shouldn’t care.

There’s a half-baked way to check if JobActivatorScope.Current is not null, but it has a few problems:

  1. It requires referencing Hangfire.Core.dll from your application logic (which you typically should avoid).
  2. It won’t work for asynchronous task-based jobs.