I am trying to schedule a task using Quartz.net 2.0 in ASP.NET MVC 4 application, but i can not get the task to be executed.
Here is the code:
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();
}
}
It is called in global.asax application start
protected void Application_Start()
{
ScheduleTaskConfig.StartScheduler();
...
}
And here is the class that implements the IJob interface:
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 ...
}
}
I placed a breakpoint at the beginning of the Execute method, but the debugger does not stop there. What am i doing wrong?
UPDATE: It has something to do with the class that implements the IJob interface not having a default constructor. It works, if I modify the constructor like this:
public QueuedEmailsSendTask()
{
}
But I need to be able to inject my dependencies. I am using the Autofac IoC container.