当我们在 ASP.NET Core 6 中使用 Quartz.AspNetCore 时,有两种方法将其托管在服务器中:
builder.Services.AddQuartz(q =>
{
});
builder.Services.AddQuartzHostedService(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
但是我们也可以做到
builder.Services.AddQuartz(q =>
{
});
builder.Services.AddQuartzServer(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
AddQuartzServer
和AddQuartzHostedService
有什么区别?
根据这篇文章,您可以发现
AddQuartz
用于添加基本的Quartz调度程序,作业和触发器配置(使用IScheduler
依赖注入容器配置和注册ASP.NET Core
实例及其相关服务)。
AddQuartzServer
用于配置它如何与asp.net core进程一起工作,它配置和注册一个托管服务,当IScheduler
应用程序启动和停止时自动启动和停止ASP.NET Core
实例。
它使用
QuartzHostedService
依赖注入容器将 ASP.NET Core
注册为托管服务。
AddQuartzServer
调用AddQuartzHostedService
,AddQuartzServer
将调用HealthChecks来检查调度程序的运行状况。
源代码如下:
using System;
using Microsoft.Extensions.DependencyInjection;
#if SUPPORTS_HEALTH_CHECKS
using Quartz.AspNetCore.HealthChecks;
#endif
namespace Quartz
{
public static class QuartzServiceCollectionExtensions
{
public static IServiceCollection AddQuartzServer(
this IServiceCollection services,
Action<QuartzHostedServiceOptions>? configure = null)
{
#if SUPPORTS_HEALTH_CHECKS
services
.AddHealthChecks()
.AddTypeActivatedCheck<QuartzHealthCheck>("quartz-scheduler");
#endif
return services.AddQuartzHostedService(configure);
}
}
}