c# - 如何在 quartz.net 调度器中使用依赖注入(inject)

标签 c# asp.net quartz-scheduler quartz.net

我正在尝试在我们使用依赖注入(inject)和服务的 asp.net mvc 4 应用程序中运行 quartz.net 服务。在这个应用程序中,我需要 quartz 来发送每日周期的电子邮件。但是奇怪的是我不能在 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; }
    }
}

最佳答案

我在 GitHub 上写了一篇简短的博客文章,并附有 YouTube 视频和源代码,展示了如何实现这一点。它带您逐步了解如何为您的作业添加依赖注入(inject),并解释它是如何工作的。该示例使用 Ninject 作为 IoC 库,但对于我放在一起以将其切换为其他内容(例如 Autofac)的代码结构来说,它应该是非常微不足道的。

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

关于c# - 如何在 quartz.net 调度器中使用依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653525/

相关文章:

c# - Hook 检测最小化窗口 C#

javascript - 如何计算定义为 'height=auto'的div的高度

java - 两个 Quartz-Worker 执行相同的作业两次

c# - Windows 服务器 2008 : Is opening smtp port not enough for sending mail?

c# - 有没有一种方法可以在 Switch 语句中执行某些代码,该语句仅在通过任何案例时才执行?

Asp.Net文本框,如何换行拆分单词而不是移动到下一行

jquery - Asp.net Webservice - 使用 jquery AJAX 安全调用 Web 服务

macos - 奇数尺寸的 CALayer 模糊

使用 Hibernate、Quartz 和 JavaMail 的应用程序中的 Java OutOfMemory

c# - 查找以大写字母开头的最后一个子字符串