嗨,我有一个后台作业,我在我的 HangfireController 中初始化,我希望该作业仅在单击按钮后触发,该作业不会触发并在我正在调用的方法内击中我的断点,并且不会出现错误,我的其他重复性工作正常工作,我会遗漏什么吗?请参阅下面的代码
挂火控制器
public class HangfireController : Controller
{
public HangFireController(my parameters...) // my constructor
{
Int SID = 0;
RecurringJob.AddOrUpdate("Import Users", () => RunLoadUsers(), "0 0,3,6,9,12,15,18,21 * * *", TimeZoneInfo.Local);
RecurringJob.AddOrUpdate("Test Example", () => TestData(), "*/15 * * * * *", TimeZoneInfo.Local); //run every 15 sec
BackgroundJob.Enqueue(()=> RunSaveStatus(SID)); //new job
}
public void RunSaveStatus(int SID)
{
//where i placed breakpoint,this point is not reached
//my logic here
}
}
家庭控制器
{
//my button calls this method when it is clicked
[HttpPut("UpdateStatus")]
public IActionResult UpdateStatus(int key, string values)
{
var SID = 10;
BackgroundJob.Enqueue<HangfireController>(x=>x.RunSaveStatus(SID));
}
}
StartUp.CS,我在其中配置 hangfire 进行初始化
string hangfireConnectionString = Configuration.GetConnectionString("myConnDB");
services.AddHangfire(configuration =>
{
configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
//.UseBatches()
//.UsePerformanceCounters()
.UseSqlServerStorage(hangfireConnectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
});
services.AddHangfireServer();
注意这两个重复的作业工作得很好,只有 Backround.enqueue 作业不会触发。