使用Autofac.Extras.Quartz时依赖注入失效问题求助
Troubleshooting Autofac.Extras.Quartz Integration Issues
Hey there, let's work through these two frustrating Autofac.Extras.Quartz problems you're dealing with—they're easy to stumble on, but we can get them sorted out.
1. Constructor Dependency Injection Not Working (Only Parameterless Constructor Gets Called)
This almost always boils down to Quartz not using Autofac's job factory to instantiate your jobs, or missing registrations for your jobs/dependencies. Here's what to check:
Fix Steps:
- Ensure you're using
AutofacJobFactoryfor Quartz
Quartz defaults to its own job factory which can't resolve constructor dependencies. You need to explicitly set Autofac's factory when initializing the scheduler:var containerBuilder = new ContainerBuilder(); // Register your job classes with Autofac (use AsSelf for concrete jobs) containerBuilder.RegisterType<MyFirstJob>().AsSelf(); containerBuilder.RegisterType<MySecondJob>().AsSelf(); // Register the Quartz-Autofac integration module containerBuilder.RegisterModule(new QuartzAutofacModule()); var container = containerBuilder.Build(); // Initialize scheduler and set the Autofac job factory var schedulerFactory = new StdSchedulerFactory(); var scheduler = await schedulerFactory.GetScheduler(); scheduler.JobFactory = new AutofacJobFactory(container); // Critical line! await scheduler.Start(); - Verify all job dependencies are registered in Autofac
If your job's constructor has dependencies that aren't registered in the container, Autofac can't create the job instance. Quartz will fall back to using the parameterless constructor if it exists (which is why that works for you). Double-check that every service your jobs depend on is registered properly.
2. Only the Last Registered Job Runs; Previous Triggers Never Fire
This usually happens when you're reusing job/trigger identities or objects, causing earlier registrations to be overwritten. Let's fix that:
Fix Steps:
- Use unique keys for every Job and Trigger
Quartz usesJobKeyandTriggerKeyto track jobs/triggers—if you reuse the same key, later registrations will overwrite earlier ones. Make sure each job and trigger has a unique identity:// First Job & Trigger var firstJob = JobBuilder.Create<MyFirstJob>() .WithIdentity(new JobKey("FirstJob", "JobGroup1")) // Unique key .Build(); var firstTrigger = TriggerBuilder.Create() .WithIdentity(new TriggerKey("FirstTrigger", "TriggerGroup1")) // Unique key .StartNow() .WithSimpleSchedule(s => s.WithIntervalInSeconds(10).RepeatForever()) .ForJob(firstJob) .Build(); await scheduler.ScheduleJob(firstJob, firstTrigger); // Second Job & Trigger var secondJob = JobBuilder.Create<MySecondJob>() .WithIdentity(new JobKey("SecondJob", "JobGroup1")) // Different key than first job .Build(); var secondTrigger = TriggerBuilder.Create() .WithIdentity(new TriggerKey("SecondTrigger", "TriggerGroup1")) // Different key than first trigger .StartNow() .WithSimpleSchedule(s => s.WithIntervalInSeconds(15).RepeatForever()) .ForJob(secondJob) .Build(); await scheduler.ScheduleJob(secondJob, secondTrigger); - Don't reuse JobDetail/Trigger instances
If you're looping to register multiple jobs, make sure you create a newJobDetailandTriggerobject for each iteration. Reusing the same instance will cause Quartz to overwrite the existing registration each time. - Start the scheduler AFTER registering all jobs/triggers
If you start the scheduler before finishing all registrations, some jobs might not be picked up correctly. Wait until all jobs and triggers are scheduled before callingawait scheduler.Start();.
Extra Troubleshooting Tips
- Enable Quartz debug logging
Turning on debug logs for Quartz will show you exactly what's happening during registration and trigger firing. You'll see if jobs are being registered, if triggers are misconfigured, or if there are resolution errors from Autofac. - Validate Autofac registrations
After building your container, usecontainer.IsRegistered<YourJobClass>()to confirm your jobs are properly registered. You can also resolve the job directly from the container to test if Autofac can instantiate it with dependencies.
内容的提问来源于stack exchange,提问作者Serge P
相关产品推荐
相关产品推荐

