I am trying to execute a Quartz scheduler job in .NET with a non-empty constructor and I try to use the default Dependency Injection of .NET to supply the dependencies. This is my job class which needs a dependency injection
public class MyJob : IJob
{
private readonly ILogger _logger;
public MyJob(ILogger<MyJob> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public Task Execute(IJobExecutionContext context)
{
_logger.LogDebug("It's working!");
}
}
And this is how I build the job
IJobDetail jobDetail = JobBuilder.Create<MyJob>()
.WithIdentity("MyID", "MyGroup")
.Build();
var triggerBuilder = TriggerBuilder.Create()
.WithIdentity("MyID")
.StartAt(DateTime.Now)
.WithCronSchedule("*/1 * * * * ?"); // Every second
var trigger = triggerBuilder.Build();
_scheduler.ScheduleJob(jobDetail, trigger)
Now, I have defined in my app configuration the following:
// Quartz configuration.
services.AddQuartz(q =>
{
// Add dependency injection.
q.UseMicrosoftDependencyInjectionScopedJobFactory(options =>
{
// if we don't have the job in DI, allow fallback
// to configure via default constructor
options.AllowDefaultConstructor = true;
});
});
services.AddTransient<MyJob>();
// Also tried services.AddTransient<IJob, MyJob>();
as defined in the documentation on DI. Yet when I rebuild my solution and run the server, the following error is thrown:
Quartz.SchedulerException: Problem instantiating class 'MyProject.MyNamespace.Myjob: Cannot instantiate type which has no empty constructor Parameter name: MyJob' ---> System.ArgumentException: Cannot instantiate type which has no empty constructor
Yet, I explicitly setup MS DI for Quartz to use following their documentation. So how can I inject dependencies? I am using Quartz 3.2.4 and I installed the Quartz.Extensions.DependencyInjection
package (also 3.2.4).