【问题标题】:How to use dependency injection in quartz.net scheduler如何在quartz.net 调度程序中使用依赖注入
【发布时间】:2015-07-27 12:47:41
【问题描述】:

我正在尝试在我们使用依赖注入和服务的 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; }
    }
}

【问题讨论】:

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


【解决方案1】:

我写了一篇简短的博文,并附有 YouTube 视频和 GitHub 上的源代码,展示了如何实现这一点。它会带您逐步了解如何为您的工作添加依赖注入,并解释它是如何工作的。该示例使用 Ninject 作为 IoC 库,但对于我将其转换为其他东西(例如 Autofac)的代码结构,它应该是非常简单的。

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

【讨论】:

  • 博文和 GitHub 源代码均已删除。这就是为什么您应该始终将代码复制到 SO,使您的答案完整且独立。链接应用作参考或附加资源,而不是作为答案本身:)
  • 谢谢,但还是不行,所以如果再次移动博客,链接将再次失败
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多