liveBook · Manning

IJob

As explained in ASP.NET Core in Action, 3e

IJob is the Quartz.NET interface that defines the executable contract for a scheduled job.

IJob defines the executable contract for a Quartz.NET scheduled job. Implement it in a C# class to place the task’s application logic in one Execute method. section 34.3.1 section 34.4

An IJob implementation separates several responsibilities:

  • The job defines what to execute, such as downloading exchange rates and saving them with EF Core. section 34.3.2
  • Triggers define when execution occurs, including immediate starts, intervals, start and end times, and recurring schedules. section 34.3.2
  • Quartz.NET creates a new job instance and scope for each execution, allowing scoped dependencies through constructor injection. section 34.3.2

An exchange-rate job

A class such as UpdateExchangeRatesJob implements IJob and receives ILogger, ExchangeRatesClient, and AppDbContext through its constructor. Register it with AddJob<T>() using a JobKey, then attach a trigger that starts immediately and repeats every five minutes. section 34.3.2

Listing 34.11 A Quartz.NET IJob for downloading and saving exchange rates
public class UpdateExchangeRatesJob : IJob                             #1
{
    private readonly ILogger<UpdateExchangeRatesJob> _logger;          #2
    private readonly ExchangeRatesClient _typedClient;                 #2
    private readonly AppDbContext _dbContext;                          #2
    public UpdateExchangeRatesJob(                                     #2
        ILogger<UpdateExchangeRatesJob> logger,                        #2
        ExchangeRatesClient typedClient,                               #2
        AppDbContext dbContext)                                        #2
    {                                                                  #2
        _logger = logger;                                              #2
        _typedClient = typedClient;                                    #2
        _dbContext = dbContext;                                        #2
    }                                                                  #2
 
    public async Task Execute(IJobExecutionContext context)            #3
    {
        _logger.LogInformation("Fetching latest rates");
        var latestRates = await _typedClient.GetLatestRatesAsync();    #4
 
        _dbContext.Add(latestRates);                                   #5
        await _dbContext.SaveChangesAsync();                           #5
 
        _logger.LogInformation("Latest rates updated");
    }
}

Inspect the complete IJob implementation pattern, including constructor-injected services and the job’s execution logic. section 34.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines one Execute method and separates task logic from trigger timing; defers scheduling, persistence, and concurrency configuration.
Examples
Uses an exchange-rate job with ExchangeRatesClient, AppDbContext, logging, and a five-minute trigger.
What you can do after
Build it from scratch by implementing Execute, registering the job, and attaching a trigger through Quartz.NET.
How it connects
Connects IJob with IHostedService, dependency injection, scoped services, triggers, JobKey, and EF Core.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.