簡體   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