使用Quartz.NET和Simple Injector进行构造函数注入

时间:2013-01-28 12:35:20

标签: c# .net dependency-injection quartz.net simple-injector

目前我正在使用Quartz.NET编写一个服务来安排它的运行。

我想知道是否有人有使用Quartz.NET和Simple Injector的构造函数注入的经验。

以下基本上是我希望实现的目标

public class JobImplementation: IJob
{
    private readonly IInjectedClass injectedClass;

    public JobImplementation(IInjectedClass _injectedClass)
    {
         injectedClass = _injectedClass
    }

    public void Execute(IJobExecutionContext _context)
    {
        //Job code
    }

3 个答案:

答案 0 :(得分:39)

根据this blog post,您需要实现自定义IJobFactory,如下所示:

public class SimpleInjectorJobFactory : IJobFactory
{
    private readonly Container container;
    private readonly Dictionary<Type, InstanceProducer> jobProducers;

    public SimpleInjectorJobFactory(
        Container container, params Assembly[] assemblies)
    {
        this.container = container;

        // By creating producers, jobs can be decorated.
        var transient = Lifestyle.Transient;
        this.jobProducers =
            container.GetTypesToRegister(typeof(IJob), assemblies).ToDictionary(
                type => type,
                type => transient.CreateProducer(typeof(IJob), type, container));
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler _)
    {
        var jobProducer = this.jobProducers[bundle.JobDetail.JobType];
        return new AsyncScopedJobDecorator(
            this.container, () => (IJob)jobProducer.GetInstance());
    }

    public void ReturnJob(IJob job)
    {
        // This will be handled automatically by Simple Injector
    }

    private sealed class AsyncScopedJobDecorator : IJob
    {
        private readonly Container container;
        private readonly Func<IJob> decorateeFactory;

        public AsyncScopedJobDecorator(
            Container container, Func<IJob> decorateeFactory)
        {
            this.container = container;
            this.decorateeFactory = decorateeFactory;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            using (AsyncScopedLifestyle.BeginScope(this.container))
            {
                var job = this.decorateeFactory();
                await job.Execute(context);
            }
        }
    }
}

此外,您还需要注册以下内容:

var container = new Container();

container.Options.ScopedLifestyle = new AsyncScopedLifestyle();

var factory = new StdSchedulerFactory();

IScheduler scheduler = await factory.GetScheduler();

scheduler.JobFactory = new SimpleInjectorJobFactory(
    container, 
    Assembly.GetExecutingAssembly()); // assemblies that contain jobs

// Optional: register some decorators
container.RegisterDecorator(typeof(IJob), typeof(LoggingJobDecorator));

container.Verify();

答案 1 :(得分:2)

晚会,但https://github.com/hbiarge/Quartz.Unity适合结合Quartz.NET和Unity。

IUnityContainer container = new UnityContainer();
container.AddNewExtension<Quartz.Unity.QuartzUnityExtension>();
// do your other Unity registrations
IScheduler scheduler = container.Resolve<IScheduler>();

scheduler.ScheduleJob(
    new JobDetailImpl(myCommandName, typeof(MyCommand)),
    TriggerBuilder.Create()
        .WithCronSchedule(myCronSchedule)
        .StartAt(startTime)
        .Build()
);
scheduler.Start();

答案 2 :(得分:0)

通过asp.net核心将Quartz.net与依赖注入引擎一起使用的步骤很少。

将nuget包添加到您的项目:

Microsoft.Extensions.DependencyInjection

创建自定义JobFactory:

public class JobFactory : IJobFactory
{
    protected readonly IServiceProvider _serviceProvider;

     public JobFactory(IServiceProvider serviceProvider) 
         => _serviceProvider = serviceProvider;

     public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
         => _serviceProvider.GetService(bundle.JobDetail.JobType) as IJob;

     public void ReturnJob(IJob job) 
         => (job as IDisposable)?.Dispose();
 }

在配置调度程序时指定JobFactory:

 var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
 scheduler.JobFactory = new JobFactory(_serviceProvider);

对于某人来说,可以在板上使用Quartz.net和DI(来自asp.net核心)获得胜利服务的有用示例:

public class WinService : ServiceBase
{
    private Scheduler _scheduleManager;

    private readonly Startup _startup;

    public WinService()
    {
        ServiceName = "SomeWinService";
        _startup = new Startup();
    }

    static void Main(string[] args)
    {
        var service = new WinService();

        // Working as Windows-service
        if (Console.IsInputRedirected && Console.IsOutputRedirected)
        {
            ServiceBase.Run(service);
        }
        // Working as console app
        else
        {
            service.OnStart(args);
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();
            service.OnStop();
        }
    }
 
    protected override void OnStart(string[] args)
    {
        _startup.RegisterServices();
        _scheduleManager = new Scheduler(_startup.ServiceProvider);
        _scheduleManager.StartTracking().Wait();
    }

    protected override void OnPause()
        => _scheduleManager.PauseTracking().Wait();

    protected override void OnContinue()
        => _scheduleManager.ResumeTracking().Wait();
 
    protected override void OnStop()
    {
        _scheduleManager.StopTracking().Wait();
        _startup.DisposeServices();
    }
}

public class Startup
{
    private IServiceProvider _serviceProvider;

    public IServiceProvider ServiceProvider => _serviceProvider;

    public void RegisterServices()
    {        
        _serviceProvider = new ServiceCollection()
            //.AddTransient(...)
            //.AddScoped(...)
            //.AddSingleton(...)
            .BuildServiceProvider();

    }

    public void DisposeServices()
    {
        if (_serviceProvider == null)
            return;

        if (_serviceProvider is IDisposable)
        {
            ((IDisposable)_serviceProvider).Dispose();
        }
    }
}

public class Scheduler
{        
    private readonly IServiceProvider _serviceProvider;
   
    private IScheduler _scheduler;
   
    public Scheduler(IServiceProvider serviceProvider) 
        => _serviceProvider = serviceProvider;
   
    public async Task StartTracking()
    {
        _scheduler = await StdSchedulerFactory.GetDefaultScheduler();
        _scheduler.JobFactory = new JobFactory(_serviceProvider);
        await _scheduler.Start();
       
        // Schedule your jobs here
    }
  
    public async Task PauseTracking() => await _scheduler?.PauseAll();
   
    public async Task ResumeTracking() => await _scheduler?.ResumeAll();
  
    public async Task StopTracking() => await _scheduler?.Shutdown();
}
相关问题