First, never use a static field for that, even if marked with a ThreadStaticAttribute
. The job could be executed on a different thread (for example, Task-based jobs), and you’ll get an empty result.
For achieving your goal, just add PerformContext
to your job method:
public void SendEmail(string name, PerformContext context)
{
string jobId = context.BackgroundJob.Id;
}
And pass null
for it when enqueuing a job:
BackgroundJob.Enqueue(() => SendEmail(name, null));
It will be substituted with a correct context when the job is actually performed.