如何在quartz.net调度程序中使用依赖注入

Bor*_*ter 5 c# asp.net quartz-scheduler quartz.net

我正在尝试在asp.net mvc 4应用程序中运行quartz.net服务,我们正在使用依赖注入和服务.在这个应用程序中,我需要石英用于每天发送电子邮件.但是奇怪的是我不能在quartz.net代码中使用DI,因为如果我添加构造函数它就会破坏它.有人有任何想法我如何解决这个问题?

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ght 6

我写了一篇简短的博客文章,附有关于GitHub的YouTube视频和源代码,展示了如何实现这一目标.它将逐步介绍如何为作业添加依赖项注入,并解释它是如何工作的.这个例子使用Ninject作为IoC库,但它应该是非常简单的代码结构我把它换成其他东西(例如Autofac).

博客文章包含视频和源代码:http://knightcodes.com/.net/2016/08/15/dependency-injection-for-quartz-net.html

  • 博文和GitHub源代码都被删除了.这就是为什么你应该总是将代码复制到SO,使你的答案完整和自包含.链接应该用作参考或aditional资源,而不是答案本身:) (2认同)