diff --git a/HangfireExample.WebService/Controllers/JobController.cs b/HangfireExample.WebService/Controllers/JobController.cs
index af86580..0800546 100644
--- a/HangfireExample.WebService/Controllers/JobController.cs
+++ b/HangfireExample.WebService/Controllers/JobController.cs
@@ -1,6 +1,8 @@
using Hangfire;
+using Hangfire.Console;
+using Hangfire.PerformContextAccessor;
+using Hangfire.Server;
using HangfireExample.WebService.Services;
-using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
@@ -10,7 +12,12 @@ namespace HangfireExample.WebService.Controllers
[ApiController]
public class JobController : ControllerBase, IJobService
{
- public JobController() { }
+ public JobController(IPerformContextAccessor perform)
+ {
+ this.perform = perform;
+ }
+
+ private readonly IPerformContextAccessor perform;
[HttpPost("Continuation", Name = "Continuation"), Produces("application/json")]
public string Continuation(string message, string jobId)
@@ -72,5 +79,37 @@ namespace HangfireExample.WebService.Controllers
{
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
}
+
+ [HttpPost("Progress", Name = "Progress"), Produces("application/json")]
+ public string Progress(int seconds)
+ {
+ return BackgroundJob.Enqueue(() => ProgressJob(perform.PerformingContext, seconds));
+ }
+
+ [NonAction]
+ [DisplayName("Progress, Seconds = {1}")]
+ public void ProgressJob(PerformContext context, int seconds)
+ {
+ var bar = context.WriteProgressBar();
+
+ context.WriteLine("The text color change to red");
+
+ context.SetTextColor(ConsoleTextColor.Red);
+
+ context.WriteLine("This progress is ready to start");
+
+ context.ResetTextColor();
+
+ foreach (var i in Enumerable.Range(0, seconds).ToList().WithProgress(bar))
+ {
+ Thread.Sleep(1000);
+ }
+
+ context.SetTextColor(ConsoleTextColor.Green);
+
+ context.WriteLine("This progress has finished");
+
+ context.ResetTextColor();
+ }
}
}
diff --git a/HangfireExample.WebService/Extensions/HangfireExtension.cs b/HangfireExample.WebService/Extensions/HangfireExtension.cs
index 0f26f85..0e775d2 100644
--- a/HangfireExample.WebService/Extensions/HangfireExtension.cs
+++ b/HangfireExample.WebService/Extensions/HangfireExtension.cs
@@ -1,5 +1,7 @@
using Hangfire;
+using Hangfire.Console;
using Hangfire.Dashboard;
+using Hangfire.PerformContextAccessor;
using Hangfire.Storage.SQLite;
using HangfireExample.WebService.Filters;
@@ -17,9 +19,13 @@ namespace HangfireExample.WebService.Extensions
///
public static IServiceCollection AddHangfire(this IServiceCollection services)
{
+ // 將 Hangfire 處理序存取加入服務
+ services.AddHangfirePerformContextAccessor();
// 將 Hangfire 加入服務,使用 SQLite 作為儲存區
services.AddHangfire(configuration => configuration
- .UseSQLiteStorage("HangfireExample.db"));
+ .UseSQLiteStorage("HangfireExample.db")
+ .UsePerformContextAccessorFilter()
+ .UseConsole());
// 將 Hangfire Server 加入服務
services.AddHangfireServer(options =>
{
@@ -38,7 +44,7 @@ namespace HangfireExample.WebService.Extensions
{
Authorization = [new DashboardAuthorizationFilter()],
// 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
- IsReadOnlyFunc = (DashboardContext context) => true,
+ IsReadOnlyFunc = (DashboardContext context) => false,
// 指定 Back to site 的連結
AppPath = "/swagger",
});
diff --git a/HangfireExample.WebService/HangfireExample.WebService.csproj b/HangfireExample.WebService/HangfireExample.WebService.csproj
index 801b9ed..d475235 100644
--- a/HangfireExample.WebService/HangfireExample.WebService.csproj
+++ b/HangfireExample.WebService/HangfireExample.WebService.csproj
@@ -7,7 +7,9 @@
+
+
diff --git a/HangfireExample.WebService/HangfireExample.db b/HangfireExample.WebService/HangfireExample.db
index 957072f..d8497c0 100644
Binary files a/HangfireExample.WebService/HangfireExample.db and b/HangfireExample.WebService/HangfireExample.db differ
diff --git a/HangfireExample.WebService/Services/IJobService.cs b/HangfireExample.WebService/Services/IJobService.cs
index 150fdc8..5733479 100644
--- a/HangfireExample.WebService/Services/IJobService.cs
+++ b/HangfireExample.WebService/Services/IJobService.cs
@@ -9,5 +9,6 @@
public string Delay(string message, int seconds, int minutes = 0, int hours = 0, int days = 0);
public string Recurring(string message, string jobId, string expression);
public string Continuation(string message, string jobId);
+ public string Progress(int seconds);
}
}
diff --git a/HangfireExample/Program.cs b/HangfireExample/Program.cs
index 8593808..05417a7 100644
--- a/HangfireExample/Program.cs
+++ b/HangfireExample/Program.cs
@@ -1,5 +1,4 @@
using Hangfire;
-using Hangfire.SqlServer;
using Hangfire.Storage.SQLite;
// 第三方擴充: Hangfire.Storage.SQLite 0.4.1