aspnetboilerplate在派生的BackgroundJob类中获取jobid

问题描述 投票:0回答:1

请帮忙。如何在BackgroundJob的派生类中获取当前作业ID?在构造函数或Execute方法中。无论如何。

Update1我以另一种方式添加后台作业。在Aspnetboilerplate文档中建议的方法。像这样:

await _backgroundJobManager.EnqueueAsync<ImportContactListJob, ImportContactListJobArgs>(
                new ImportContactListJobArgs(){someargs});

Then my method stars from overriding method execute in BackgroundJob inheritance class.

public override void Execute(ImportContactListJobArgs args)
     {
     
//job here
}
    

我无法在EnqueueAsync中传递null值,因为没有重载方法。

aspnetboilerplate
1个回答
0
投票

这没有内置的方法。

但你可以使用ABP的Hangfire Integration并做this

为了实现您的目标,只需将PerformContext添加到您的工作方法:

public void SendEmail(string name, PerformContext context)
{
   string jobId = context.BackgroundJob.Id;
}

在排队工作时通过null

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

在实际执行作业时,它将被正确的上下文替换。

Update

我无法在EnqueueAsync中传递null值,因为没有重载方法。

PerformContext放在ImportContactListJob的构造函数中。

© www.soinside.com 2019 - 2024. All rights reserved.