参照了:https://www.cnblogs.com/LaoPaoEr/p/15129899.html
1.项目Nuget引用Quartz.AspNetCore和Quartz.Extensions.DependencyInjection,3.3.3 及以上版本。
2.新建一个MyTestJob类:
using Quartz; using System; using System.IO; using System.Text; using System.Threading.Tasks; namespace AspNetCoreQuartz333.QuartzJob { [DisallowConcurrentExecution] public class MyTestJob : IJob { public MyTestJob() { //构造函数里可注入 } public Task Execute(IJobExecutionContext context) { return Task.Run(() => { using (StreamWriter sw = new StreamWriter(@"d:/Quartz-NET.txt", true, Encoding.UTF8)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff =>") + "1"); } }); } } }
3.修改Startup-ConfigureServices方法,加入以下内容:
string thisJob = "TestJob"; string groupName = "gp" + thisJob; string jobName = "job" + thisJob; string triggerName = "trigger" + thisJob; //Quartz的工作单元 services.AddTransient<MyTestJob>(); //Quartz调度中心 services.AddQuartz(q => { //用于注入 q.UseMicrosoftDependencyInjectionJobFactory(); // 基本Quartz调度器、作业和触发器配置 var jobKey = new JobKey(jobName, groupName); q.AddJob<MyTestJob>(jobKey, j => j .WithDescription("My work") ); q.AddTrigger(t => t .WithIdentity(triggerName) .ForJob(jobKey) .StartNow() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s .RepeatForever())//持续工作 .WithDescription("My work trigger") ); }); // ASP.NET核心托管-添加Quartz服务器 services.AddQuartzServer(options => { // 关闭时,我们希望作业正常完成 options.WaitForJobsToComplete = false; });
Startup完整代码:
using AspNetCoreQuartz333.QuartzJob; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace AspNetCoreQuartz333 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); //避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法 QuartzConfigureServices(services); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } /// <summary> /// 避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法 /// </summary> /// <param name="services"></param> public void QuartzConfigureServices(IServiceCollection services) { #region Quartz string thisJob = "TestJob"; string groupName = "gp" + thisJob; string jobName = "job" + thisJob; string triggerName = "trigger" + thisJob; //Quartz的工作单元 services.AddTransient<MyTestJob>(); //Quartz调度中心 services.AddQuartz(q => { //用于注入 q.UseMicrosoftDependencyInjectionJobFactory(); // 基本Quartz调度器、作业和触发器配置 var jobKey = new JobKey(jobName, groupName); q.AddJob<MyTestJob>(jobKey, j => j .WithDescription("My work") ); q.AddTrigger(t => t .WithIdentity(triggerName) .ForJob(jobKey) .StartNow() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s .RepeatForever())//持续工作 .WithDescription("My work trigger") ); }); // ASP.NET核心托管-添加Quartz服务器 services.AddQuartzServer(options => { // 关闭时,我们希望作业正常完成 options.WaitForJobsToComplete = false; }); #endregion } } }
END