我正在尝试在 ASP.NET MVC 4 应用程序中使用 Quartz.net 2.0 安排任务,但我无法执行任务。
这是代码:
public class ScheduleTaskConfig
{
public static void StartScheduler()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
JobKey emailSenderTaskKey = new JobKey("emailSenderTask", "email");
IJobDetail emailSenderTask = JobBuilder.Create<QueuedEmailsSendTask>()
.WithIdentity(emailSenderTaskKey)
.Build();
TriggerKey emailSenderTriggerKey = new TriggerKey("emailSenderTrigger", "email");
ITrigger emailSenderTrigger = TriggerBuilder.Create()
.WithIdentity(emailSenderTriggerKey)
.WithSimpleSchedule(s => s.RepeatForever().WithIntervalInSeconds(5))
.StartNow()
.Build();
scheduler.ScheduleJob(emailSenderTask, emailSenderTrigger);
scheduler.Start();
}
}
它在 global.asax 应用程序启动中调用
protected void Application_Start()
{
ScheduleTaskConfig.StartScheduler();
...
}
这是实现 IJob 接口的类:
public class QueuedEmailsSendTask : IJob
{
private IQueuedEmailsService _queuedEmailsService { get; set; }
private IEmailSenderService _emailSenderService { get; set; }
public QueuedEmailsSendTask(IQueuedEmailsService queuedEmailsService, IEmailSenderService emailSenderService)
{
this._queuedEmailsService = queuedEmailsService;
this._emailSenderService = emailSenderService;
}
public void Execute(IJobExecutionContext executeContext)
{
//do stuff ...
}
}
我在 Execute 方法的开头放置了一个断点,但调试器并没有停在那里。我究竟做错了什么?
更新: 它与实现没有默认构造函数的 IJob 接口的类有关。如果我像这样修改构造函数,它会起作用:
public QueuedEmailsSendTask()
{
}
但我需要能够注入我的依赖项。我正在使用 Autofac IoC 容器。