0

I have just started using Quartz on my asp.net core web API project but I can't figure out how to access my database/context on my quartz class that implements IJob, when I try to add the context to my constructor the job doesn't get scheduled anymore and doesn't run at all, although it should run every 5 seconds, here is my old job class which works fine and get scheduled to run fine every 5 seconds:

class BatchJobCheckContract : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        var msg = "\n  --  Contract checking job executed at : " + DateTime.Now.ToString();
        Debug.WriteLine(msg);
    }
}

and here is how I changed it to access my database but doesn't work anymore :

class BatchJobCheckContract : IJob
{
    private readonly RHPDbContext _context;

    public BatchJobCheckContract(RHPDbContext context)
    {
        _context = context;
    }
    
    public async Task Execute(IJobExecutionContext context)
    {
        var msg = "\n  --  Contract checking job executed at : " + DateTime.Now.ToString();
        Debug.WriteLine(msg);

       var workers = await _context.Workers
                                    .Include(w => w.ContactInformationGroup.ContactInformations)
                                    .Include(w => w.AddressGroup.Addresses)
                                    .Include(w => w.CalculationProfilGroup.CalculationProfil)
                                    .Include(w => w.Contracts)
                                    .Include(w => w.Absences).ThenInclude(x => x.AbsenceGroup)
                                    .ToListAsync();

        Debug.WriteLine(workers.Count);

    }
}

what am I doing wrong? how can I access the context the right way?

  • 1
    looks like the default job factory could not create a job whose class does not expose a parameterless constructor. You need to create your own custom job factory class. Also note that because the job runs in background, the DbContext should be created in the scope of the job (not shared with the request scope). – Hopeless Nov 18 '20 at 10:50
  • thanks for the comment, can you please elaborate on how I can do that? any links or examples, I can check? – YOUSFI Mohamed Walid Nov 18 '20 at 11:27
  • you can search for "Quartz.net custom job factory". This is a bit challenging when it comes to inject scoped dependency (here the `IJobExecutionContext`) into your job. I've done this once myself. However you can try googling with appropriate keywords, someone surely encountered this same scenario (using a DbContext in a Quartz.net job) – Hopeless Nov 18 '20 at 11:35

1 Answers1

1

Quartz.NET has built-in support for hosting on ASP.NET Core. Please have a look at the documentation how to configure your setup to utilize dependency injection.

Marko Lahma
  • 6,586
  • 25
  • 29
  • Short answers like this aren't very helpful, even though they're correct. I'm trying to find a duplicate for [this question](https://stackoverflow.com/questions/76407344/servicelifetime-transient-dbcontext-causes-memory-leak) and all I can find are these short answers. There's nothing (even in the docs) that shows a beginner how to register and use a DbContext in Quartz.NET in a concise way – Panagiotis Kanavos Jun 20 '23 at 08:28