How to kick off static `BackgroundJob` methods and write unit tests

Moved to http://docs.hangfire.io/en/latest/users-guide/background-methods/writing-unit-tests.html

just what i was looking for, can we use IOC to inject IBackgroundJobClient ?

Of course, the IBackgroundJobClient interface and BackgroundJobClient implementation is like other dependencies that you register to use in controllers.

But be aware that some IoC controllers uses the most concrete constructor (with the largest number of parameters), if you you register the type only and do not choose the right constructor. BackgroundJobClient ctor has the following overloads:

  • BackgroundJobClient(JobStorage storage)
  • BackgroundJobClient(JobStorage storage, IStateMachineFactory stateMachineFactory)
  • BackgroundJobClient(JobStorage storage, IStateMachineFactory stateMachineFactory, IJobCreationProcess process).

And you sometimes need to register either these implementations:

  • JobStorage type → JobStorage.Current instance.
  • IStateMachineFactorynew StateMachineFactory() instance (it is thread-safe).
  • IJobCreationProcessJobCreationProcess.Instance.

thanks for reply, this clear things up, i think it is a a good title for a tutorial of how to integrate Hangfire with IOC.

Whoops, I missed something: don’t forget to call IBackgroundJobClient.Dispose method! HangFire uses connection pool to get connections, and the absence of this call may lead to connection pool contention!

Already added issue #98 for this.

thank you for the hint