背景
之前.net core使用quartz.net时,总感觉非常变扭,百度和谷歌了N久都没解决以下问题,造成代码丑陋,非常不优雅:
1.项目启动时,要立刻恢复执行quartz.net中的任务 2.quartz.net中的Job任务无法使用ioc注入,要额外写一套 直到最近看到这篇文章,终于解决了我的问题,特此记录一下前后代码对比
Program.cs
前查看详细内容public class Program{ public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); QuartzFactory.StartSpider(); host.Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup();}
后
查看详细内容class Program{ static async Task Main(string[] args) { var host = new HostBuilder() .ConfigureHostConfiguration(configHost => { // 配置根目录 configHost.SetBasePath(Path.GetDirectoryName(typeof(Program).Assembly.Location)); // 读取环境变量,Asp.Net core默认的环境变量是以ASPNETCORE_作为前缀的,这里也采用此前缀以保持一致 configHost.AddEnvironmentVariables("ASPNETCORE_"); // 读取启动host的时候之前可传入参数 configHost.AddCommandLine(args); }) .ConfigureAppConfiguration((hostContext, configApp) => { // 读取应用的配置json configApp.AddJsonFile("appsettings.json", true); // 读取应用特定环境下的配置json configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true); // 读取环境变量 configApp.AddEnvironmentVariables(); }) .ConfigureServices((hostContext, services) => { // 添加日志Service services.AddLogging(); // 设置efcore连接字符串 services.AddDbContext(options => options.UseSqlite(hostContext.Configuration.GetConnectionString("demoConnection"))); services.AddSingleton (); services.AddSingleton(provider => { StdSchedulerFactory factory = new StdSchedulerFactory(); var scheduler = factory.GetScheduler().ConfigureAwait(false).GetAwaiter().GetResult(); scheduler.JobFactory = provider.GetService (); return scheduler; }); // 添加自定义的HostedService services.AddHostedService (); services.AddHostedService (); services.AddSingleton (); }) .ConfigureLogging((hostContext, configLogging) => { // 输出控制台日志 configLogging.AddConsole(); // 输出Debug日志 configLogging.AddDebug(); }) // 使用控制台生命周期 .UseConsoleLifetime() .Build(); await host.RunAsync(); }}
Job
前查看详细内容public class SpiderJob : IJob{ public async Task Execute(IJobExecutionContext context) { using (var ctx = new EfContext()) { var list = await ctx.Goods.AsNoTracking().ToListAsync(); } }}
后
查看详细内容public class TestJob : IJob{ private readonly ILogger _logger; private readonly IConfiguration _config; private readonly EFContext _context; public TestJob(ILoggerlogger, IConfiguration config, EFContext context) { _logger = logger; _config = config; _context = context; } public async Task Execute(IJobExecutionContext context) { _logger.LogInformation(string.Format("[{0:yyyy-MM-dd hh:mm:ss:ffffff}]任务执行!", DateTime.Now)); // 读取配置 Console.WriteLine($"读取配置{ _config["test"] }"); // 读取数据库 var model = await _context.QuartInfo.FirstOrDefaultAsync(); if (model != null) { Console.WriteLine($"读取数据库 { model.jobName }"); } }}
Demo地址: