Autofac integration package for Quartz.Net.
Autofac.Extras.Quartz creates nested litefime scope for each Quartz Job. Nested scope is disposed after job execution has been completed.
This allows to have single instance per job execution as well as deterministic disposal of resources.
Install package via Nuget: install-package Autofac.Extras.Quartz
Stable | Pre-release | |
---|---|---|
Build | ||
NuGet | ||
CodeCov |
Autofac configuration for Quartz includes two steps:
- Scheduler registration
- Job registration
QuartzAutofacFactoryModule
registers custom ISchedulerFactory
and default instance of IScheduler
in Autofac container.
Both factory and schedulere are registered as singletones.
Note: Is is important to resolve IScheduler
from container, rather than using default one to get jobs resolved by Autofac.
Optionally custom Quartz configuration can be passed using ConfigurationProvider
property. Provider is callback which returns settings using NameValueCollection
.
Starting with version 7 QuartzAutofacFactoryModule
provides a way to customize lifetime scope configuration for job. This can be done via JobScopeConfigurator
parameter.
cb.Register(_ => new ScopedDependency("global"))
.AsImplementedInterfaces()
.SingleInstance();
cb.RegisterModule(new QuartzAutofacFactoryModule {
JobScopeConfigurator = (builder, jobScopeTag) => {
// override dependency for job scope
builder.Register(_ => new ScopedDependency("job-local "+ DateTime.UtcNow.ToLongTimeString()))
.AsImplementedInterfaces()
.InstancePerMatchingLifetimeScope(jobScopeTag);
}
});
See src/Samples/Shared/Bootstrap.cs for details.
QuartzAutofacJobsModule
scans given assemblies and registers all non-abstract implementors of IJob
interface as transient instances.
internal static ContainerBuilder ConfigureContainer(ContainerBuilder cb)
{
// 1) Register IScheduler
cb.RegisterModule(new QuartzAutofacFactoryModule());
// 2) Register jobs
cb.RegisterModule(new QuartzAutofacJobsModule(typeof (CleanupExpiredAnnouncemetsJob).Assembly));
}
Make sure to start the scheduler after it was resolved from Autofac. E.g.
var scheduler = _container.Resolve<IScheduler>();
scheduler.Start();
- See src/Samples/Console for .NetCore console application.
- src/Samples/Shared contains source code shared between samples.
TopShelf-based sample was removed since Topshelf.Quartz is not compatible with Quartz 3 as af now.