How to use dependency injection in quartz.net scheduler

I am trying to start the quartz.net service in an asp.net mvc 4 application where we use dependency injection and services. In this application, I need quartz to send letters with a daily period. But it’s strange that I can’t use DI in quartz.net code because it broke if I add a constructor to it. Does anyone have any ideas how I can solve this 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 = "someemail@mail.com",
                    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; }
    }
}
+4
source share
1 answer

YouTube GitHub, , . , , , . Ninject IoC, , , - (, Autofac).

: http://knightcodes.com/.net/2016/08/15/dependency-injection-for-quartz-net.html

+6

All Articles