因爲發佈網站上Quartz.net的定時調度會被IIS回收不執行,解決的方法寫Windows服務-案列【天天早晨8點刪除過時35天的域名】數據庫
整了個三成架構:從Nuget成程序管理包下載Quart類庫和TopShelf類庫windows
1.建立定時調度Quartz類架構
1 class QuartzServiceRunner 2 { 3 private readonly IScheduler scheduler; 4 5 public QuartzServiceRunner() 6 { 7 scheduler = StdSchedulerFactory.GetDefaultScheduler(); 8 } 9 10 public void Start() 11 { 12 //從配置文件中讀取任務啓動時間 13 string cronExpr = ConfigurationManager.AppSettings["cronExpr"]; 14 IJobDetail job = JobBuilder.Create<DeleteDomainJob>().WithIdentity("job1", "group1").Build(); 15 //建立任務運行的觸發器 16 ITrigger trigger = TriggerBuilder.Create() 17 .WithIdentity("triggger1", "group1") 18 .WithSchedule(CronScheduleBuilder.CronSchedule(new CronExpression(cronExpr))) 19 .Build(); 20 //啓動任務 21 scheduler.ScheduleJob(job, trigger); 22 scheduler.Start(); 23 scheduler.Start(); 24 25 } 26 27 public void Stop() 28 { 29 scheduler.Clear(); 30 } 31 32 public bool Continue(HostControl hostControl) 33 { 34 scheduler.ResumeAll(); 35 return true; 36 } 37 38 public bool Pause(HostControl hostControl) 39 { 40 scheduler.PauseAll(); 41 return true; 42 } 43 44 }
2.建立從數據庫中查詢過時的域名執行刪除方法的調度類繼承IJob接口dom
class DeleteDomainJob : IJob { readonly ILog _log = LogManager.GetLogger(typeof(DeleteDomainJob)); public void Execute(IJobExecutionContext context) { _log.InfoFormat("域名刪除調度開始******"); DomainService service = new DomainService(); int outDay = Convert.ToInt16(ConfigurationManager.AppSettings["outDay"]); List<DeleteDoaminModel> list = service.QueryDeleteDomain(outDay); StringBuilder sb = new StringBuilder(); foreach (var list_domain in list) { string str = ""; bool flag = DeleteDomainService.delete(list_domain.Domain, ref str); _log.InfoFormat("要刪除的域名=" + list_domain.Domain + "到期時間=" + list_domain.EndDateUP + "訂單編號=" + list_domain.DomainOrderID + "成功標誌=" + flag + "返回信息=" + str); if (flag) { sb.Append("'" + list_domain.Domain + "',"); } } string str_domain = sb.ToString().TrimEnd(','); if (str_domain.Length > 0) { bool flag = service.Logic_Delete(str_domain); _log.InfoFormat("數據庫刪除域名===" + str_domain + "刪除標誌==" + flag); } } }
3.主函數入口函數
class Program { public static void Main(string[] args) { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x => { x.Service<QuartzServiceRunner>(s => { s.ConstructUsing(name => new QuartzServiceRunner()); s.WhenStarted(tc => tc.Start()); s.WhenStopped(tc => tc.Stop()); }); x.RunAsLocalSystem(); x.StartAutomatically(); x.SetDescription("開創雲調度服務"); x.SetDisplayName("開創雲調度服務"); x.SetServiceName("開創雲調度服務"); }); } }
4.生成Windows服務工具
把你的項目Release生成找到解決方案所在的文件夾網站
找到***windowservice.exe;複製當前的路徑G:\TopshelfDemo\TopshelfDemo\bin\Release\****.WindowService.exeui
以管理員的身份運行命令提示符窗口spa
安裝完成找到管理工具》服務程序 便可看到.net