|
|
@@ -12,8 +12,8 @@ using Hangfire;
|
|
12
|
12
|
using WebApplication1.Filter;
|
|
13
|
13
|
using NLog.Extensions.Logging;
|
|
14
|
14
|
using NLog.Web;
|
|
15
|
|
-
|
|
16
|
15
|
using System.Transactions;
|
|
|
16
|
+using StackExchange.Redis;
|
|
17
|
17
|
|
|
18
|
18
|
namespace WebApplication1
|
|
19
|
19
|
{
|
|
|
@@ -30,24 +30,19 @@ namespace WebApplication1
|
|
30
|
30
|
public void ConfigureServices(IServiceCollection services)
|
|
31
|
31
|
{
|
|
32
|
32
|
services.AddMvc();
|
|
33
|
|
- services.AddHangfire(x => x.UseSqlServerStorage("Data Source=.;User ID=sa;pwd=123456;Initial Catalog=HangFire;"));
|
|
34
|
|
-
|
|
35
|
|
-
|
|
36
|
|
- //services.AddHangfire(x => x.UseStorage(new MySqlStorage("Driver={MySQL};Server=localhost;Option=16834;Database=hangfire;", new MySqlStorageOptions
|
|
37
|
|
- //{
|
|
38
|
|
- // TransactionIsolationLevel = IsolationLevel.ReadCommitted,
|
|
39
|
|
- // QueuePollInterval = TimeSpan.FromSeconds(15),
|
|
40
|
|
- // JobExpirationCheckInterval = TimeSpan.FromHours(1),
|
|
41
|
|
- // CountersAggregateInterval = TimeSpan.FromMinutes(5),
|
|
42
|
|
- // PrepareSchemaIfNecessary = true,
|
|
43
|
|
- // DashboardJobListLimit = 50000,
|
|
44
|
|
- // TransactionTimeout = TimeSpan.FromMinutes(1),
|
|
45
|
|
- //}
|
|
46
|
|
- //)));
|
|
47
|
33
|
|
|
|
34
|
+ //添加 Hangfire 组件,并且指定使用 SQL Server 进行持久化存储
|
|
|
35
|
+ services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("hangfire.sqlserver")));
|
|
48
|
36
|
|
|
|
37
|
+ //添加 Hangfire 组件,并且指定使用 Redis 进行持久化存储
|
|
|
38
|
+ services.AddHangfire(x =>
|
|
|
39
|
+ {
|
|
|
40
|
+ //从appsettings中获取 Redis 连接字符串
|
|
|
41
|
+ x.UseRedisStorage(ConnectionMultiplexer.Connect(Configuration.GetConnectionString("hangfire.redis")));
|
|
|
42
|
+ });
|
|
49
|
43
|
|
|
50
|
44
|
|
|
|
45
|
+ //添加日志组件
|
|
51
|
46
|
services.AddLogging();
|
|
52
|
47
|
}
|
|
53
|
48
|
|
|
|
@@ -62,24 +57,26 @@ namespace WebApplication1
|
|
62
|
57
|
|
|
63
|
58
|
|
|
64
|
59
|
#region HangFire
|
|
|
60
|
+ //启动hangfire面板
|
|
|
61
|
+ app.UseHangfireDashboard();
|
|
|
62
|
+ //配置hangfire面板访问权限
|
|
|
63
|
+ var options = new DashboardOptions
|
|
|
64
|
+ {
|
|
|
65
|
+ Authorization = new[] { new HangfireAuthorizationFilter() }
|
|
|
66
|
+ };
|
|
|
67
|
+ app.UseHangfireDashboard("/hangfire", options);
|
|
65
|
68
|
|
|
66
|
|
- app.UseHangfireDashboard();//启动hangfire面板
|
|
67
|
|
-
|
|
|
69
|
+ //hangfire 配置项
|
|
68
|
70
|
var jobOptions = new BackgroundJobServerOptions
|
|
69
|
71
|
{
|
|
70
|
72
|
Queues = new[] { "test", "default" },//队列名称,只能为小写
|
|
71
|
73
|
WorkerCount = Environment.ProcessorCount * 5, //并发任务数
|
|
72
|
74
|
ServerName = "hangfire1",//服务器名称
|
|
|
75
|
+
|
|
73
|
76
|
};
|
|
74
|
77
|
//启动Hangfire服务
|
|
75
|
78
|
app.UseHangfireServer(jobOptions);
|
|
76
|
79
|
|
|
77
|
|
- var options = new DashboardOptions
|
|
78
|
|
- {
|
|
79
|
|
- Authorization = new[] { new HangfireAuthorizationFilter() }
|
|
80
|
|
- };
|
|
81
|
|
- app.UseHangfireDashboard("/hangfire", options);
|
|
82
|
|
-
|
|
83
|
80
|
#endregion
|
|
84
|
81
|
|
|
85
|
82
|
#region NLog
|