Autofac and Quartz.Net Integration

萝らか妹 提交于 2019-11-30 11:26:43

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!