Skip to content

Installation ​

Install AlmightyShogun.Hangfire.RecurringJobs in the application that should configure Hangfire and schedule recurring jobs from attributes. The package targets net10.0, sets Hangfire up with in-memory storage unless another provider is selected, and discovers job classes that implement IRecurringJob.

sh
dotnet add package AlmightyShogun.Hangfire.RecurringJobs

Dependencies ​

Package references ​

  • Hangfire 1.8.25 — provides the background job server, recurring job manager, and job metadata types.
  • Hangfire.InMemory 1.0.0 — backs the default storage AddCustomHangfire selects. It stays referenced even when another provider is used, which the consuming project's dependency graph shows.
  • Newtonsoft.Json 13.0.4 — Hangfire's serializer, which arrives transitively and shows up in the consuming project's dependency graph.
  • Cronos 0.13.0 — validates cron expressions on recurring-job attributes when the host starts.

Project references ​

  • AlmightyShogun.Utils — provides assembly scanning and inherited-type registration helpers.

Startup Registration ​

AddCustomHangfire sets up Hangfire with a processing server, using in-memory storage unless a configuration delegate selects another provider. RegisterRecurringJobs scans for job classes and adds the hosted service that puts their schedules into Hangfire when the host starts.

WARNING

Both calls are needed, in either order. Neither reads what the other registered: the scan only records what to schedule, and the hosted service it adds hands those schedules to the recurring job manager once the host starts.

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

builder.Services
    .AddCustomHangfire()
    .RegisterRecurringJobs();
csharp
using AlmightyShogun.Hangfire.RecurringJobs;

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

All packages are released under the MIT License.