如何在派生的BackgroundJob类中获取Jobid? [英] How to get jobid inside derived BackgroundJob class?

查看:105
本文介绍了如何在派生的BackgroundJob类中获取Jobid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆