【发布时间】:2018-01-17 11:59:05
【问题描述】:
我正在尝试使用 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);
}
}
}
【问题讨论】:
标签: c# asp.net-core scheduled-tasks fluentscheduler