A convenient way to create Quartz.NET jobs using attributes.
- Supports scheduling jobs using
QuartzJobAttribute
instead of configuration files or programming. - Supports dependency injection in
IJob
class (depends on the library Quartz.Extensions.DependencyInjection). - Follows the lifecycle of ASP .NET Core.
Install it:
# Install by Package Manager
> Install-Package Quartz.AspNetCore
> Install-Package SKIT.Quartz.Extensions.JobAttribute
# Install by .NET CLI
> dotnet add package Quartz.AspNetCore
> dotnet add package SKIT.Quartz.Extensions.JobAttribute
Here is an example:
using Quartz;
[QuartzJob(Name = "Clocking", Description = "Tell the time every seconds.", CronExpression = "* * * * * ? ")]
public class ClockingJob : IJob
{
private readonly ILogger _logger;
public ClockingJob(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
}
Task IJob.Execute(IJobExecutionContext context)
{
_logger.LogInformation("Now is " + context.FireTimeUtc);
return Task.CompletedTask;
}
}
Also, there are options for Name, Group, Description CronExpression, Priority, StoreDurably and RequestRecovery.
Then register service in ConfigureServices method in Startup.cs
file.
using Quartz;
public void ConfigureServices(IServiceCollection services)
{
services.AddQuartzJobs();
services.AddQuartzServer(options =>
{
options.WaitForJobsToComplete = true;
});
}