是否有静态hangfire上下文getter,类似于HttpContext.Current
Is there a static hangfire context getter, similar to HttpContext.Current
我有一个现有的 api,它按线程存储数据并使用 HttpContext.Current
检索。
我正在尝试重构此 class 以从 hangfire 作业调用 -- 我想知道是否有等效的静态方法来检索 hangfire 执行上下文。
如果不是,我也想知道hangfire作业和线程之间是否存在1:1关系。我找不到任何关于 hangfire 作业生命周期的文档——即 threadstart -> job start -> job end -> thread dispose
,或者如果 1 个线程可以同时处理多个作业,即 threadstart -> job1 start, job2 start, job3 start, job1 end, job4 start,job2 end, job1 end, job3 end -> thread dispose
来自 - https://discuss.hangfire.io/t/how-to-get-jobid-within-job/851/4
a [ThreadStatic]
变量将在 ServerFilter
中发挥作用
public class JobContext : IServerFilter
{
[ThreadStatic]
private static string _jobId;
public static string JobId { get { return _jobId; } set { _jobId = value; } }
public void OnPerforming(PerformingContext context)
{
JobId = context.BackgroundJobId;
}
}
// And register it
GlobalConfiguration.Configuration.UseFilter(new JobContext());
遇到这个寻找其他东西,更新的方法(适用于 1.6.20,不确定它能工作多久)是在你的方法中有一个 Server.PerformContext 类型的参数表达式调用和 hangfire 将自动设置它(就像站点关闭的取消标记)
如果你原谅 VB 代码,我有这个工作方法的签名
<DisplayName("{0}")> 'use jobname param as the name of the job
Sub RunJob(jobName As String, configID as Integer hfContext As Server.PerformContext, cancellationToken As Hangfire.IJobCancellationToken)
我用
创建工作
Dim jobExpression As Linq.Expressions.Expression(Of Action(Of HangfireJob)) = Sub(x) x.RunJob(opt.JobName, opt.configID, Nothing, JobCancellationToken.Null)
RecurringJob.AddOrUpdate(Of HangfireJob)(opt.SchedulerSystemJobID, jobExpression, opt.RecurringCronSchedule, tzinfo)
然后在 RunJob 方法中获取我使用的 ID
hfContext.BackgroundJob.Id
我有一个现有的 api,它按线程存储数据并使用 HttpContext.Current
检索。
我正在尝试重构此 class 以从 hangfire 作业调用 -- 我想知道是否有等效的静态方法来检索 hangfire 执行上下文。
如果不是,我也想知道hangfire作业和线程之间是否存在1:1关系。我找不到任何关于 hangfire 作业生命周期的文档——即 threadstart -> job start -> job end -> thread dispose
,或者如果 1 个线程可以同时处理多个作业,即 threadstart -> job1 start, job2 start, job3 start, job1 end, job4 start,job2 end, job1 end, job3 end -> thread dispose
来自 - https://discuss.hangfire.io/t/how-to-get-jobid-within-job/851/4
a [ThreadStatic]
变量将在 ServerFilter
public class JobContext : IServerFilter
{
[ThreadStatic]
private static string _jobId;
public static string JobId { get { return _jobId; } set { _jobId = value; } }
public void OnPerforming(PerformingContext context)
{
JobId = context.BackgroundJobId;
}
}
// And register it
GlobalConfiguration.Configuration.UseFilter(new JobContext());
遇到这个寻找其他东西,更新的方法(适用于 1.6.20,不确定它能工作多久)是在你的方法中有一个 Server.PerformContext 类型的参数表达式调用和 hangfire 将自动设置它(就像站点关闭的取消标记)
如果你原谅 VB 代码,我有这个工作方法的签名
<DisplayName("{0}")> 'use jobname param as the name of the job
Sub RunJob(jobName As String, configID as Integer hfContext As Server.PerformContext, cancellationToken As Hangfire.IJobCancellationToken)
我用
创建工作 Dim jobExpression As Linq.Expressions.Expression(Of Action(Of HangfireJob)) = Sub(x) x.RunJob(opt.JobName, opt.configID, Nothing, JobCancellationToken.Null)
RecurringJob.AddOrUpdate(Of HangfireJob)(opt.SchedulerSystemJobID, jobExpression, opt.RecurringCronSchedule, tzinfo)
然后在 RunJob 方法中获取我使用的 ID
hfContext.BackgroundJob.Id