Hangfire 是一個簡單的用於.net及.net core 應用程式,通過資料庫持久化,定時執行後臺任務的組件 1、通過NuGet安裝Hangfire 2、在Startup.cs文件的ConfigureServices方法裡面配置Hangfire服務,設置Hangfire服務使用的資料庫 3、在 ...
Hangfire 是一個簡單的用於.net及.net core 應用程式,通過資料庫持久化,定時執行後臺任務的組件
1、通過NuGet安裝Hangfire
2、在Startup.cs文件的ConfigureServices方法裡面配置Hangfire服務,設置Hangfire服務使用的資料庫
public void ConfigureServices(IServiceCollection services) { services.AddHangfire(a => a.UseSqlServerStorage("Data Source=.\\SQLExpress;Initial Catalog=Hangfire;User ID=sa;Password=sa")); //services.AddMvc(); }
3、在Configure方法裡面加入Hangfire服務和Hangfire的界面控制台
app.UseHangfireServer();
app.UseHangfireDashboard();
4、運行應用程式,資料庫自動生成了相關的表
5、打開http://localhost:port/hangfire,就可以看到Hangfire的控制界面了
6、在程式啟動時添加一個每分鐘迴圈執行的定時任務
RecurringJob.AddOrUpdate(() => Console.WriteLine("執行任務了...."), Cron.Minutely());
7、運行程式,在控制台就可以看到剛纔加入的定期作業
hangfire的網站:https://www.hangfire.io/