Hangfire 如何将从HttpContext检索到的值传递给BackgroundJob.Enqueue
我的控制器中有以下代码:Hangfire 如何将从HttpContext检索到的值传递给BackgroundJob.Enqueue,hangfire,Hangfire,我的控制器中有以下代码: var user = HttpContext == null ? null : HttpContext.Items["ApplicationUser"] as ApplicationUser; int organisationid = user == null ? 0 : user.OrganisationID; BackgroundJob
var user = HttpContext == null ? null
: HttpContext.Items["ApplicationUser"] as ApplicationUser;
int organisationid = user == null ? 0
: user.OrganisationID;
BackgroundJob.Enqueue(() => Merge(organisationid));
因此后台作业调用控制器中的合并方法:
public void Merge(int organisationid)
{
_Uow.MergeSHQSCostsTable(organisationid);
}
但是我希望merge方法中的调用使用从请求中检索的
organizationid
,并且在调用Enqueue
时可用。我该怎么做?当前,我的代码总是通过值零,因为运行后台作业时,HttpContext
为空。诀窍是用单独的方法封装对BackgroundJob.Enqueue的调用,以便Hangfire可以序列化参数:
public SHQSController(IUnitOfWork uow) : base(uow)
{
//CurrentOrganisationID is retrieved from HttpContext
EnqueueMerge(CurrentOrganisationID);
}
private void EnqueueMerge(int organisationid)
{
BackgroundJob.Enqueue(() => Merge(organisationid));
}