c# - How to use dependency injection in quartz.net scheduler -
i'm trying run quartz.net service in asp.net mvc 4 application using dependency injection & services. in application need quartz sending emails daily period. whats strange can't use di in quartz.net code because it's broke if i'm add constructor it. have ideas how can resolve 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() { // instance of quartz.net scheduler ischeduler scheduler = stdschedulerfactory.getdefaultscheduler(); scheduler.start(); // start scheduler if in standby if(!scheduler.isstarted) scheduler.start(); // define job scheduled var job = jobbuilder.create<emailjob>() .withidentity("jobmonthschedulerseventhday", "it") .requestrecovery() .build(); // associate trigger job var trigger = (icrontrigger)triggerbuilder.create() .withidentity("triggermonthschedulerseventhday", "it") //.withcronschedule("0 0 12 7 1/1 ? *") // visit http://www.cronmaker.com/ queues job every minute .withcronschedule("0 0/1 * 1/1 * ? *") .startat(datetime.utcnow) .withpriority(1) .build(); // validate job doesn't 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 short blog post accompanied youtube video , source code on github showing how accomplish this. takes step step through how add dependency injection jobs, , explains how works. example uses ninject ioc library, should pretty trivial structure of code put switch out else (e.g. autofac).
blog post video , source code: http://knightcodes.com/.net/2016/08/15/dependency-injection-for-quartz-net.html
Comments
Post a Comment