asp.net-mvc - 每天在特定时间进行Hangfire定期性工作

标签 asp.net-mvc background-process hangfire recurring

我试图每天在9:00 AM执行hangfire定期工作。这是我想做的-

RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");

我应该把这行代码放在哪里?

抱歉,这是一个愚蠢的问题。

最佳答案

假设您正在使用.Net Core,您可以在其中找到文件startup.cs。在那您可以找到Configure()方法。在该方法内部,您可以在app.UseHangfireDashboard()app.UseHangfireServer()之后使用该行,用于配置hangfire仪表板,这是可选的。别忘了使用ConfigureServices()本身中的startup.cs方法注册Hangfire服务

您可以在 Startup.cs 中的ConfigureServices()注册Hangfire服务

    public void ConfigureServices(IServiceCollection services)
    {    
    /*
    other services
    */

    services.AddHangfire(x => x.UseSqlServerStorage("YOUR_HangfireServerConnectionString"));

    /*
    services.AddMvc()
    */
    }

您可以在 Startup.cs 中的Configure()设置Hangfire Cron
public void Configure(IApplicationBuilder app)
{
    app.UseHangfireDashboard();  
    app.UseHangfireServer();
    RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");
}

有关更多信息,请引用link

更新

cron表达式* 9 * * *表示作业将在系统时间UTC时间9(24小时格式)之后每分钟触发一次。

要在每天的9:00 AM创建周期性工作,表达式应为0 9 * * *,请参见此处cron expressions

关于asp.net-mvc - 每天在特定时间进行Hangfire定期性工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49627893/

相关文章:

java - 为什么我的 Android 应用程序偶尔会非常快地耗尽电池电量?

ios - 后台任务有限制吗?

Hangfire 作业超时

asp.net-mvc - 如何从mvc3调用存储过程?

cordova - 使用 AngularJS 和 Ionic 访问 PhoneGap 中运行的后台进程

c# - 运行时 ASP.net 的 IIS 应用程序名称

c# - 使用带 DTC 的 MSMQ 时,Hangfire 作业会在 1 分钟后自动重新安排

hangfire - 是否有类似于 HttpContext.Current 的静态 hangfire 上下文 getter

asp.net-mvc - MVC.net 在 View 中获取枚举显示名称,而无需在 View 中引用枚举类型

c# - 在 For 循环而不是 Foreach Asp MVC 5 中显示模型结果