闽公网安备 35020302035485号
支持多种消息队列:Hangfire 支持多种消息队列,如 SQL Server、RabbitMQ 和 Redis 等,这为用户提供了更多的选择和灵活性。
Install-Package Hangfire.AspNetCore Install-Package Hangfire.MySqlStorage2. 配置 Hangfire 使用 MySQL 存储
builder.Services.AddHangfire(config =>
{
config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings();
config.UseStorage(
new MySqlStorage(
@"server=127.0.0.1;user=root;password=123456;database=hangfire;allowuservariables=True;",
//必须允许用户自定义参数,不然面板不能使用
new MySqlStorageOptions()));
});
builder.Services.AddHangfireServer();
builder.Services.AddTransient<IBackgroundJobClient, BackgroundJobClient>();
确保将 MySQL 连接字符串替换为你自己的数据库连接信息。// 堆代码 duidaima.com app.UseHangfireDashboard();现在,你可以通过访问 /hangfire 路径来查看 Hangfire Dashboard。

Ignorable:指定不应在错过的计划中创建任何后台作业,无论错过的次数如何。
WeatherForecast weather = new WeatherForecast(); BackgroundJob.Enqueue(() => HelloJob.Test4(weather));延迟作业
BackgroundJob.Schedule(() => HelloJob.Test2("Test2"), TimeSpan.FromMinutes(1));
重复作业 RecurringJob.AddOrUpdate("static-job", () => HelloJob.Test2(DateTime.Now.ToLongTimeString()), "0 * * * * ?", new RecurringJobOptions()
{
MisfireHandling = MisfireHandlingMode.Ignorable,
TimeZone = TimeZoneInfo.Local,
});
RecurringJob.AddOrUpdate<IOCJob>("ioc-job", job => job.Test(), "0 * * * * ?");
延续作业var jobId = BackgroundJob.Enqueue(() => HelloJob.Test1());
BackgroundJob.ContinueJobWith(jobId, () => HelloJob.Test2("Test3"));
附录:
hangfire官网地址:https://www.hangfire.io/