When we use Quartz.AspNetCore with ASP.NET Core 6, there are two ways to host it in the server:
builder.Services.AddQuartz(q =>
{
});
builder.Services.AddQuartzHostedService(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
But we can do it as well with
builder.Services.AddQuartz(q =>
{
});
builder.Services.AddQuartzServer(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
what difference between AddQuartzServer
and AddQuartzHostedService
?
According to this article, you could find the AddQuartz
is used to add base Quartz scheduler, job and trigger configuration(configure and register the IScheduler
instance and its related services with the ASP.NET Core
dependency injection container).
The AddQuartzServer
is used to configure how it works with asp.net core process , it configure and register a hosted service that starts and stops the IScheduler
instance automatically when the ASP.NET Core
application starts and stops.
It registers QuartzHostedService
as a hosted service with the ASP.NET Core
dependency injection container.
The AddQuartzServer
call the AddQuartzHostedService
, the AddQuartzServer
will call the HealthChecks to check the scheduler health.
Source code like this:
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);
}
}
}