我正在使用Quartz.Net windows服務在具有Castle windsor依賴注入的MVC4應用程序上運行遠程作業。我想安排在執行參考模型接口上訪問模型函數的作業。Quartz.Net使用DI註冊作業
這是我的工作類:
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,ISchedulerF actory和StdSchedulerFactory實例並創建作業。
container.Register(Component.For<IQuartzSchedulingServiceSettings>()
.UsingFactoryMethod(QuartzSchedulingServiceConfiguration.GetConfiguration)
.LifeStyle.PerWebRequest);
container.Register(Component.For<IQuartzTaskSchedulingService>()
.ImplementedBy<QuartzTaskSchedulingService>()
.LifeStyle.PerWebRequest);
當我嘗試在我的Quartz.Net窗口服務日誌執行MyJob我得到以下錯誤:
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執行作業和日程表它。只要它執行我得到以下問題
我已經嘗試了很多解決方案來得到這個工作,但他們都已經結束unsuccesfuly 我是否缺少某種類型的註冊或我做錯了什麼?
謝謝!
可以請你展示如何執行這項工作? – Cybermaxs
Cybermaxs - Betclic,我編輯帖子回答你的問題。謝謝 – user3170957