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:
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:
_scheduler = StdSchedulerFactory.GetDefaultScheduler()
to configure all jobs (which all are started correctly)If package does not work, then custom AutofacJobFactory will be implemented.
You can try to create jobs with custom IJobFactory.
scheduler.JobFactory = new JobFactoryInjection(container);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With