Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection does not work if Autofac.Extras.Quartz is used

Description

I read a lot how to configure Autofac.Extras.Quartz to add Autofac integration, but it simply does not work, I struggle with such issues:

  1. Dependency injection via constructor does not work (but if I add parameterless constructor - it is called)
  2. Last registered job is only called, triggers which added before last job - never fired

I spent a lot of time to figure out how to start it properly (which looks simple but not workable in my case), the most relevant to my case closed issue of project where people had the same issues, but going through it did not help. Any help is appreciated.

Code:

...
// Configure Autofac
var builder = new ContainerBuilder();
...
builder.RegisterModule<AutofacRepositoryModule>(); // registration of repositores
...
builder.RegisterModule(new QuartzAutofacFactoryModule());
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(ConvertOrdersJob).Assembly));
builder.RegisterType<JobScheduler>().AsSelf();
var container = builder.Build();
ConfigureScheduler(container);
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
...

private static void ConfigureScheduler(IContainer container)
{
    var scheduler = container.Resolve<JobScheduler>();
    scheduler.Start();
}

Registration of repositories in AutofacRepositoryModule:

public class AutofacRepositoryModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        ...
        builder.RegisterType<DumpReportRepository>().As<IDumpReportRepository>();
        ...
    }
}

Configure jobs:

public class JobScheduler
{
    private IScheduler _scheduler;

    public JobScheduler(IScheduler scheduler)
    {
        _scheduler = scheduler;
    }

    public void Start()
    {
        // BUG1: Find out why cleanupDumpsTrigger never fired
        // BUG2: Dependency injection via constructor does not work
        var cleanupDumpsJob = JobBuilder.Create<DumpsCleanupJob>().Build();
        var cleanupDumpsTrigger = TriggerBuilder.Create()
            .WithIdentity("CleanupDumpsTrigger", "dumps")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInHours(24)
                .RepeatForever())
            .Build();
        _scheduler.ScheduleJob(cleanupDumpsJob, cleanupDumpsTrigger);

        var convertOrdersJob = JobBuilder.Create<ConvertOrdersJob>().Build();
        var convertOrdersTrigger = TriggerBuilder.Create()
            .WithIdentity("ConvertOrdersTrigger", "orders")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInHours(1)
                .RepeatForever())
            .Build();
        _scheduler.ScheduleJob(convertOrdersJob, convertOrdersTrigger);

        _scheduler.Start();
    }

Example of job:

[DisallowConcurrentExecution]
public class DumpsCleanupJob : IJob
{
    private readonly IDumpReportRepository _dumpReportRepository;
    public DumpsCleanupJob(IDumpReportRepository dumpReportRepository)
    {
       // BUG: Constructor is never called  
        _dumpReportRepository = dumpReportRepository;
    }
    public void Execute(IJobExecutionContext context)
    {
        try
        {
            ...
        }
        catch (Exception ex)
        {
            ...
        }
    }
}

Versions:

I tried bunch of versions of Quartz (2.x-3.x)/Autofac.Extras.Quartz (3.x-4.0.0)

Workaround

Temporary solution:

  1. Usage of Autofac.Extras.Quartz was removed
  2. All dependencies are simply created in parameterless constructor
  3. Default scheduler is used _scheduler = StdSchedulerFactory.GetDefaultScheduler() to configure all jobs (which all are started correctly)

If package does not work, then custom AutofacJobFactory will be implemented.

like image 479
Serge P Avatar asked Nov 18 '22 14:11

Serge P


1 Answers

You can try to create jobs with custom IJobFactory.

scheduler.JobFactory = new JobFactoryInjection(container);
like image 89
Kirill Starodubtsev Avatar answered Apr 21 '23 03:04

Kirill Starodubtsev