繁体   English   中英

在ASP.NET MVC Action方法内部调用时,Hangfire作业无法触发

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM