Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac and Quartz.Net Integration

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 an ILifetimeScope into the IJob and have the Execute method create a child ILifetimeScope 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 as Owned<T>, or Func<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.

like image 338
David Faivre Avatar asked Feb 05 '11 22:02

David Faivre


2 Answers

Without knowing too much about Quartz.Net and IJobs, 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.

like image 69
Peter Lillevold Avatar answered Nov 17 '22 02:11

Peter Lillevold


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:)

like image 23
shatl Avatar answered Nov 17 '22 03:11

shatl