Installation
Install AlmightyShogun.Hangfire.Utils in the application that should configure Hangfire and schedule recurring jobs from attributes. The package targets net10.0, registers Hangfire with in-memory storage, and discovers job classes that implement IRecurringJob.
sh
dotnet add package AlmightyShogun.Hangfire.UtilsDependencies
Hangfire1.8.23— provides the background job server, recurring job manager, and job metadata types.Hangfire.InMemory1.0.0— provides the in-memory Hangfire storage used byAddHangfire.Newtonsoft.Json13.0.4— dependency used by Hangfire serialization.AlmightyShogun.Utilsproject reference — provides assembly scanning and inherited-type registration helpers.
Startup Registration
Register Hangfire first, then register recurring job classes from the assemblies that contain them. Add JobSchedulerStartup as a hosted service when the application should schedule discovered jobs during startup.
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;
}
}