Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Hangfire job id in the code

Tags:

c#

jobs

hangfire

I'm doing a Fire-and-Forget method invocation like this.

BackgroundJob.Enqueue(() => SendEmail(name));

I would like to pass the job id to the SendEmail() method and use it inside the method.

like image 506
Aki T Avatar asked Jan 18 '26 15:01

Aki T


2 Answers

The easy way is to add

PerformContext

to the job method:. Hangfire Forum topic Use Hangfire job id in the code.

like image 60
Aki T Avatar answered Jan 20 '26 04:01

Aki T


Change the SendEmail method to look like this...

public void SendEmail(string name, string jobId)
{ 
//Body of method
}

Change the Hangfire call to look like this...

BackgroundJob.Enqueue((string jobId) => SendEmail(name, jobId));

Unless, you're looking to run a job, and send an email one complete, then you might have to do the following...

  var jobId = BackgroundJob.Enqueue(() => TaskToRun);
  BackgroundJob.ContinueWith(jobId, () => SendEmail(name, jobId));
like image 36
Christian Phillips Avatar answered Jan 20 '26 06:01

Christian Phillips



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!