如何在派生的BackgroundJob类中获取Jobid? [英] How to get jobid inside derived BackgroundJob class?
问题描述
如何在BackgroundJob
的派生类中获取当前的工作ID?
在构造函数或Execute
方法中.
可以.
How to get current job id inside derived class of BackgroundJob
?
In constructor or Execute
method.
Any way.
更新1
我以另一种方式添加后台作业. ASP.NET Boilerplate文档中建议的方法.像这样:
I'm adding background job another way. The approach that suggested on ASP.NET Boilerplate documentation. Like this:
await _backgroundJobManager.EnqueueAsync<ImportContactListJob, ImportContactListJobArgs>(
new ImportContactListJobArgs(){someargs});
然后,我的方法从覆盖BackgroundJob
继承类中的方法Execute
开始.
Then my method starts from overriding method Execute
in BackgroundJob
inheritance class.
public override void Execute(ImportContactListJobArgs args)
{
// job here
}
我无法在EnqueueAsync
中传递null
值,因为对此没有重载方法.
I can't pass null
value in EnqueueAsync
because there is no overloaded method for this.
推荐答案
没有内置的方法.
但是您可以使用ABP的 Hangfire集成并执行
But you can use ABP's Hangfire Integration and do this:
要实现您的目标,只需在您的工作方法中添加
PerformContext
:
public void SendEmail(string name, PerformContext context)
{
string jobId = context.BackgroundJob.Id;
}
并在排队工作时将null
传递给它:
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.
更新
我无法在EnqueueAsync中传递空值,因为对此没有重载方法.
I can't pass null value in EnqueueAsync because there is no overloaded method for this.
将PerformContext
放入ImportContactListJob
的构造函数中.
相关: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/4444
这篇关于如何在派生的BackgroundJob类中获取Jobid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!