Generate a MethodCallExpression to pass to HangFire Enqueue

You cannot use pure lambda expressions to enqueue work. Hangfire needs to serialize data about the classes that will actually implement the method, as well as the parameters you are passing to your method.

BackgroundJob.Enqueue<Worker>(w => w.DoWork(DateTime.Now, JobParam1));

You need to specify the type, like “Worker”, then in the lambda expression you can call suitable methods, passing the parameters required as necessary.

This allows you to externalise the “processor” from the application enqueing items. The enqueuer can fire-and-forget, but in order for the “processor” to understand the queued task, it needs to know the type (so it can load it) and the parameters.

1 Like