Quartz.Net使用DI注册作业

时间:2014-01-07 23:01:26

标签: c# asp.net-mvc-4 dependency-injection quartz.net

我正在使用Quartz.Net Windows服务在带有Castle windsor依赖注入的MVC4应用程序上运行远程作业。我想安排在执行参考模型接口上访问模型函数的作业。

这是我的Job类:

    public class MyJob: IJob 
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(MyJob));
        private readonly IQuartzModel _quartzModel;

        public MyJob(IQuartzModel quartzModel)
        {
           this._quartzModel = quartzModel;
        }

       public void Execute(IJobExecutionContext context)
       {
            quartzModel.DoModelFunction();       
       }
  }

我像这样创建自己的IJobFactory实例:

public class WindsorJobFactory : IJobFactory
{
    private readonly IWindsorContainer _container;

    public WindsorJobFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle)
    {
        if (bundle == null)
        {
            throw new ArgumentNullException("bundle");
        }
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);
    }
 }

我用castle DI

创建了这些注册
        IJobFactory jobFactory = new WindsorJobFactory(container);

        container.Register(Component.For<IJobFactory>().Instance(jobFactory));
        container.Register(Component.For<IQuartzModel>().ImplementedBy<QuartzModel>());

        var jobTypes = GetJobTypes();
        foreach (Type jobType in jobTypes)
        {
              container.Register(Component.For(jobType).ImplementedBy(jobType).LifeStyle.Transient);
        }

    }

    private static IEnumerable<Type> GetJobTypes()
    {
        return AppDomain.CurrentDomain.GetAssemblies().ToList()
            .SelectMany(s => s.GetTypes())
            .Where(p => typeof(IJob).IsAssignableFrom(p) && !p.IsInterface);
    }

此外,我还为调度程序服务设置和QuartzTaskSchedulingService注册,我在其中创建IScheduler,ISchedulerFactory和StdSchedulerFactory实例并创建作业。

        container.Register(Component.For<IQuartzSchedulingServiceSettings>()
                        .UsingFactoryMethod(QuartzSchedulingServiceConfiguration.GetConfiguration)
                        .LifeStyle.PerWebRequest);

        container.Register(Component.For<IQuartzTaskSchedulingService>()
                  .ImplementedBy<QuartzTaskSchedulingService>()
                  .LifeStyle.PerWebRequest);

当我尝试执行MyJob时,我在Quartz.Net Windows服务日志中收到以下错误:

System.ArgumentException: Cannot instantiate type which has no empty constructor

如果我在构造函数中没有传递接口的情况下执行MyJob,它就会成功执行。

要运行作业,我从我的控制器初始化QuartzTaskSchedulingService并执行作业创建方法。在QuartzTaskSchedulingService里面我有ISchedulerFactory,IScheduler的初始化。在构造函数内部,我使用IQuartzSchedulingServiceSettings提供的连接设置来获取调度程序的实例。

    public QuartzTaskSchedulingService(IQuartzSchedulingServiceSettings settings)
    {
        this.settings = settings;

        Address = string.Format("tcp://{0}:{1}/{2}", settings.ServerAddress, settings.ServerPort, settings.SchedulerBindName);
        schedulerFactory = new StdSchedulerFactory(GetProperties(Address, settings.ServerInstanceName));

            try
            {
                quartzScheduler = schedulerFactory.GetScheduler();
                if (!quartzScheduler.IsStarted)
                {
                    quartzScheduler.Start();
                }
            }
      }

之后,它跳转到创造就业方法

    public void TestJobShot(Type t)
    {

        IJobDetail job = JobBuilder.Create(t)
            .WithIdentity("MyJob", "Common")
            .RequestRecovery()
            .Build();

        var trigger = (ICronTrigger)TriggerBuilder.Create()
            .WithIdentity("MyJob", "Common")
            .WithCronSchedule("0 0/1 * 1/1 * ? *")
            .StartAt(DateTime.UtcNow)
            .WithPriority(1)
            .Build();

        quartzScheduler.ScheduleJob(job, trigger);

    }

它成功地创建了作业并安排从AdoJobStore执行。一旦执行我就会遇到以下问题

我已经尝试了许多解决方案来实现这一目标,但所有这些解决方案都已经结束了 我错过了某种DI注册,还是我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我没弄错的话,你的容器没有正确解析IQuartzModel。 我有同样类型的问题,但使用Autofac。