在 HangFire 中设置特定的日期和时间

Saj*_*del 5 c# asp.net scheduler hangfire

如何设定具体时间?例如 2021 年 7 月 2 日上午 8:00。
我应该使用哪些方法?Enqueue方法还是Schedule方法还是AddOrUpdate方法?

She*_*ari 6

Hangfire 使用cron 计划表达式。您可以设置任何所需的日期时间表达式。\n例如

\n
\n

0 8 24 7 * =>\n\xe2\x80\x9c7 月 24 日 08:00。\xe2\x80\x9d

\n
\n

下面的代码展示了如何使用表达式,您可以使用一些在线工具来创建表达式,例如crontab

\n
RecurringJob.AddOrUpdate(jobId, methodCall, "0 8 24 7 *", TimeZoneInfo.Local);\n
Run Code Online (Sandbox Code Playgroud)\n

如果你想执行一次某件事,你应该使用BackgroundJob。

\n
var selectedDate = DateTimeOffset.Parse("2021-07-02 08:00:00");\nBackgroundJob.Schedule(() => YourDelayedJob(), selectedDate);\n
Run Code Online (Sandbox Code Playgroud)\n