[英]Hangfire job not firing when called inside of ASP.NET MVC Action method
將hangfire作業放入MVC中的Index方法中時,它不會啟動。 我不知道為什么。
[HttpGet]
public IActionResult Index()
{
RecurringJob.AddOrUpdate(() => GetCurrentUserNotifications(User.Identity.Name), Cron.MinuteInterval(1));
return View(vm);
}
public void GetCurrentUserNotifications(string userId)
{
_connectionManager.GetHubContext<NotificationsHub>()
.Clients.All.broadcastNotifications(_repository.GetNotifications()
.Where(x => x.DateTime <= DateTime.Now && x.CreatedBy == userId));
}
問題是您的方法GetCurrentUserNotifications(string userId)
是您的控制器類的方法。 當Hangfire執行作業時,它將嘗試創建控制器類的實例。 但我相信您的控制器類沒有參數少的構造函數。 因此它將無法執行作業。 解決方案是創建一個單獨的類,例如BackgroundProcess
。 將您的GetCurrentUserNotifications
放入其中,並按如下所示進行調用:
RecurringJob.AddOrUpdate(() => new BackgroundProcess().GetCurrentUserNotifications(User.Identity.Name), Cron.MinuteInterval(1));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.