Skip to content

RegisterRecurringJobs

Registers recurring job services and metadata from one or more assemblies. When no assembly is provided, the method uses the calling assembly.

Use this method after AddHangfire and pass the assembly that contains recurring job classes. Each discovered job class must implement IRecurringJob, be marked with RecurringJobAttribute, and expose a public parameterless RunAsync method that returns Task. The method registers matching job classes as scoped services and stores their RecurringJob metadata for JobSchedulerStartup.

Usage

csharp
using AlmightyShogun.Hangfire.Utils;
using Microsoft.Extensions.DependencyInjection;

builder.Services
    .AddHangfire()
    .RegisterRecurringJobs(typeof(Program).Assembly)
    .AddHostedService<JobSchedulerStartup>();
csharp
using AlmightyShogun.Hangfire.Utils;

[RecurringJob("cleanup-expired-sessions", "0 */6 * * *")]
public sealed class CleanupExpiredSessionsJob : IRecurringJob
{
    public Task RunAsync()
    {
        return Task.CompletedTask;
    }
}

Parameters

assemblies: Assembly[]
Assemblies used when registering recurring job-related services. When omitted, the calling assembly is used.
Default: []

Returns

The IServiceCollection instance with recurring job registrations applied.

Type signature

csharp
public IServiceCollection RegisterRecurringJobs(
    params Assembly[] assemblies
);

Uses

All packages are released under the MIT License.