I am experimenting with FluentScheduler for some background tasks in ASP.net Core API.
The job should send push notifications every day at a particular time interval based on few criteria. I had gone through the document and implemented a test functionality to print some output in the console window. It worked as expected with predicted time interval.
But the actual job I am going to do with that involves database context which provides necessary information to perform the criteria to send out the notifications.
My problem is I am unable to use constructor with parameter in MyJob
class which is throwing missing method exception
PS: As per this article from Scott Hanselman, FluentScheduler seems to be quite famous but I could not get any help from online communities. But obviously, it's quite easy to grasp.
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);
}
}
}