I'm trying to run up quartz.net service in asp.net mvc 4 application where we are using dependency injection & services. In this application I need quartz for sending emails with daily period. But whats the strange I can't use DI in quartz.net code because it's broke if I'm add constructor to it. Is anybody have any ideas how I can resolve that problem?
namespace BBWT.Web.Scheduler {
public class EmailJob : IJob {
//private readonly IEmailSender emailSender;
//public EmailJob(IEmailSender emailSender) {
// this.emailSender = emailSender;
//}
public void Execute(IJobExecutionContext context) {
var result = new List<DebtorsDTO>()
{
new DebtorsDTO()
{
InvoiceId = 1,
ClientName = "Some Client",
ClientEmail = "[email protected]",
ClientId = 1,
//SessionId = 1,
Date = "17.07.2015",
TutorName = "Tutor Tutor",
InvoiceName = "Invoice Name 1",
AgedAnalysis = "some analysis",
SevedDaysOverdue = 1000,
FourteenDaysOverdue = 0,
TwentyOneDaysOverdue = 0,
MoreThatTwentyEightDaysOverdue = 0,
Status = "Sent 7 day reminder",
IsSelectForEmail = false,
OnChase = true,
OnHold = false
}
};
//string[] lines = { "First line", "Second line", "Third line" };
//System.IO.File.WriteAllLines(@"D:\Test\Test.txt", lines);
}
}
public class JobScheduler {
public static void Start() {
// Get an instance of the Quartz.Net scheduler
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
// Start the scheduler if its in standby
if(!scheduler.IsStarted)
scheduler.Start();
// Define the Job to be scheduled
var job = JobBuilder.Create<EmailJob>()
.WithIdentity("JobMonthSchedulerSeventhDay", "IT")
.RequestRecovery()
.Build();
// Associate a trigger with the Job
var trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("TriggerMonthSchedulerSeventhDay", "IT")
//.WithCronSchedule("0 0 12 7 1/1 ? *") // visit http://www.cronmaker.com/ Queues the job every minute
.WithCronSchedule("0 0/1 * 1/1 * ? *")
.StartAt(DateTime.UtcNow)
.WithPriority(1)
.Build();
// Validate that the job doesn't already exists
if(scheduler.CheckExists(new JobKey("JobMonthSchedulerSeventhDay", "IT"))) {
scheduler.DeleteJob(new JobKey("JobMonthSchedulerSeventhDay", "IT"));
}
var schedule = scheduler.ScheduleJob(job, trigger);
}
}
public class EmailSenderFormDTO {
public string Name { get; set; }
public string Surname { get; set; }
public string EmailTo { get; set; }
}
}
I wrote up a short blog post accompanied by a YouTube video and source code on GitHub showing how to accomplish this. It takes you step by step through how to add dependency injection for your jobs, and explains how it all works. The example uses Ninject as the IoC library, but it should be pretty trivial with structure of the code I put together to switch that out for something else (e.g. Autofac).
Blog Post with video and source code: http://knightcodes.com/.net/2016/08/15/dependency-injection-for-quartz-net.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With