quartz .net8微服务避免dbcontext异常示例

 

调整program.cs里注册QuartzJobService,去除掉原有框架内的QuartzCore文件夹中的Factory middlejob等方法

// 注册QuartzJobService
builder.Services.AddQuartz(q =>
{
    var jobKey = new JobKey("Jobs");
    q.AddJob<InterAutoTicketing>(opts => opts.WithIdentity(jobKey));


    //var jobKey2 = new JobKey("Jobs2");

    //q.AddJob<InterAutoTicketingTest>(opts => opts.WithIdentity(jobKey2));

    var quartzThreadCount = ConfigHelper.GetSectionValue("QuartzThreadCount");
    var maxThreadCount = string.IsNullOrEmpty(quartzThreadCount) ? 3 : Convert.ToInt32(quartzThreadCount);
    q.UseDefaultThreadPool(tp => tp.MaxConcurrency = maxThreadCount);

    var cron = ConfigHelper.GetSectionValue("QuartzCron") ?? "0/1 * * * * ? ";
    q.AddTrigger(opts => opts
        .ForJob(jobKey)
        .WithIdentity($"{nameof(InterAutoTicketingTest)}-trigger")
        .WithCronSchedule(cron)
    );
    //var cron = ConfigHelper.GetSectionValue("QuartzCron") ?? "0/1 * * * * ? ";
    //q.AddTrigger(opts => opts
    //.ForJob(jobKey2)
    //.WithIdentity("InterAutoTicketingTest-trigger")
    //.WithCronSchedule(cron));
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);

  

posted on 2024-11-19 10:37  御行所  阅读(34)  评论(0)    收藏  举报

导航