- HangFire可以達成異步處理程序的工具,自帶儀表板可以隨時監控程序狀態。
HangFire需運行在owin上
1
2
3
4
5
6
7//// owin相關
Microsoft.Owin
Microsoft.Owin.Diagnostics
Microsoft.Owin.Host.HttpListener
Microsoft.Owin.Hosting
//// Storage使用redis 其餘會自動相依
Hangfire.Redis.StackExchange建構Startup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19using Hangfire;
using Hangfire.Redis;
using HangFireRedis.Applibs;
using Owin;
public class Startup
{
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration
.UseRedisStorage(NoSqlService.RedisConnections);
//// 啟用HanfireServer
app.UseHangfireServer();
//// 啟用Hangfire的Dashboard
app.UseHangfireDashboard();
}
}HangFire主要功能
Fire-and-forget tasks (射後不理)
1
BackgroundJob.Enqueue(Expression<Action methodCall)
Delayed tasks (延遲)
1
BackgroundJob.Schedule(Expression<Action> methodCall, TimeSpan delay)
Recurring tasks (定時 / 週期)
1
RecurringJob.AddOrUpdate(string recurringJobId, Expression<Action> methodCall, string cronExpression)