[英]Quartz.Net registering jobs with DI
我正在使用Quartz.Net Windows服務通過Castle Windsor依賴項注入在MVC4應用程序上運行遠程作業。 我想安排在執行參考模型接口上訪問模型函數的作業。
這是我的工作班級:
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);
}
}
我已經用城堡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注冊,或者我做錯了什么?
謝謝!
如果我沒記錯的話,那么您的容器無法正確解析IQuartzModel。 我遇到了相同類型的問題,但使用的是Autofac。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.