我正在用 FluentScheduler 试验 ASP.net Core API 中的一些后台任务。
该作业应根据几个标准在特定时间间隔每天发送推送通知。我已经浏览了文档并实现了一个测试功能以在控制台窗口中打印一些输出。它按预期的时间间隔工作。
但我要做的实际工作涉及数据库上下文,它提供必要的信息来执行发送通知的标准。
我的问题是我无法在MyJob
类中使用带参数的构造函数,这会引发缺少方法异常
PS:根据 Scott Hanselman 的这篇文章,FluentScheduler 似乎很有名,但我无法从在线社区获得任何帮助。但显然,它很容易掌握。
public class MyJob : IJob
{
private ApplicationDbContext _context;
public MyJob(ApplicationDbContext context)
{
_context = context;
}
public void Execute()
{
Console.WriteLine("Executed");
SendNotificationAsync();
}
private async Task SendNotificationAsync()
{
var overdues = _context.Borrow.Join(
_context.ApplicationUser,
b => b.ApplicationUserId,
a => a.Id,
(a, b) => new { a, b })
.Where(z => (z.a.ReturnedDate == null) && (z.a.BorrowApproval == 1))
.Where(z => z.a.ReturnDate.Date == new DateTime().Date.AddDays(1).Date)
.Select(z => new { z.a.ApplicationUserId, z.a.Book.ShortTitle, z.a.BorrowedDate, z.b.Token })
.ToList();
Console.WriteLine("Acknowledged");
foreach (var r in overdues)
{
string message = "You are running late! The book '" + r.ShortTitle + "' borrowed on '" + r.BorrowedDate + "' due tomorrow.";
Console.WriteLine(message);
await new PushNotificationService().sendAsync(r.Token, "Due Tomorrow!", message);
}
}
}