[英]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.