问题
Does anyone have any experience integrating autofac and Quartz.Net? If so, where is it best to control lifetime management -- the IJobFactory, within the Execute of the IJob, or through event listeners?
Right now, I'm using a custom autofac IJobFactory
to create the IJob
instances, but I don't have an easy way to plug in to a ILifetimeScope
in the IJobFactory to ensure any expensive resources that are injected in the IJob are cleaned up. The job factory just creates an instance of a job and returns it. Here are my current ideas (hopefully there are better ones...)
It looks like most AutoFac integrations somehow wrap a
ILifetimeScope
around the unit of work they create. The obvious brute force way seems to be to pass anILifetimeScope
into theIJob
and have theExecute
method create a childILifetimeScope
and instantiate any dependencies there. This seems a little too close to a service locator pattern, which in turn seems to go against the spirit of autofac, but it might be the most obvious way to ensure proper handling of a scope.I could plug into some of the Quartz events to handle the different phases of the Job execution stack, and handle lifetime management there. That would probably be a lot more work, but possibly worth it if it gets cleaner separation of concerns.
Ensure that an IJob is a simple wrapper around an
IServiceComponent
type, which would do all the work, and request it asOwned<T>
, orFunc<Owned<T>>
. I like how this seems to vibe more with autofac, but I don't like that its not strictly enforceable for all implementors of IJob.
回答1:
Without knowing too much about Quartz.Net and IJob
s, I'll venture a suggestion still.
Consider the following Job wrapper:
public class JobWrapper<T>: IJob where T:IJob
{
private Func<Owned<T>> _jobFactory;
public JobWrapper(Func<Owned<T>> jobFactory)
{
_jobFactory = jobFactory;
}
void IJob.Execute()
{
using (var ownedJob = _jobFactory())
{
var theJob = ownedJob.Value;
theJob.Execute();
}
}
}
Given the following registrations:
builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<SomeJob>();
A job factory could now resolve this wrapper:
var job = _container.Resolve<JobWrapper<SomeJob>>();
Note: a lifetime scope will be created as part of the ownedJob
instance, which in this case is of type Owned<SomeJob>
. Any dependencies required by SomeJob
that is InstancePerLifetimeScope
or InstancePerDependency
will be created and destroyed along with the Owned
instance.
回答2:
Take a look at https://github.com/alphacloud/Autofac.Extras.Quartz. It also available as NuGet package https://www.nuget.org/packages/Autofac.Extras.Quartz/
I know it a bit late:)
来源:https://stackoverflow.com/questions/4910131/autofac-and-quartz-net-integration