From 634172304f3b74a926f91280854e020b39f57577 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Tue, 12 Mar 2024 09:43:29 +0800 Subject: [PATCH] 113/03/12 --- .../Extensions/HangfireExtension.cs | 31 ++++++++++++++++++ .../HangfireExample.WebService.csproj | 19 +++++++++++ HangfireExample.WebService/HangfireExample.db | Bin 0 -> 159744 bytes HangfireExample.WebService/Program.cs | 29 ++++++++++++++++ .../Properties/launchSettings.json | 31 ++++++++++++++++++ .../appsettings.Development.json | 8 +++++ HangfireExample.WebService/appsettings.json | 10 ++++++ HangfireExample.sln | 8 ++++- HangfireExample/Program.cs | 6 +++- README.md | 5 +-- 10 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 HangfireExample.WebService/Extensions/HangfireExtension.cs create mode 100644 HangfireExample.WebService/HangfireExample.WebService.csproj create mode 100644 HangfireExample.WebService/HangfireExample.db create mode 100644 HangfireExample.WebService/Program.cs create mode 100644 HangfireExample.WebService/Properties/launchSettings.json create mode 100644 HangfireExample.WebService/appsettings.Development.json create mode 100644 HangfireExample.WebService/appsettings.json diff --git a/HangfireExample.WebService/Extensions/HangfireExtension.cs b/HangfireExample.WebService/Extensions/HangfireExtension.cs new file mode 100644 index 0000000..013efbe --- /dev/null +++ b/HangfireExample.WebService/Extensions/HangfireExtension.cs @@ -0,0 +1,31 @@ +using Hangfire; +using Hangfire.Storage.SQLite; + +namespace HangfireExample.WebService.Extensions +{ + /// + /// Hangfire 的擴充方法。 + /// + public static class HangfireExtension + { + /// + /// 加入 Hangfire 服務。 + /// + /// + /// + public static IServiceCollection AddHangfire(this IServiceCollection services) + { + // 將 Hangfire 加入服務,使用 SQLite 作為儲存區 + services.AddHangfire(configuration => configuration + .UseSQLiteStorage("HangfireExample.db")); + return services; + } + + public static WebApplication UseHangfireDashboard(this WebApplication app, IConfiguration configuration) + { + // HangfireDashboard 預設路由為 /Hangfire + app.UseHangfireDashboard(configuration.GetValue("DashboardRoot")); + return app; + } + } +} diff --git a/HangfireExample.WebService/HangfireExample.WebService.csproj b/HangfireExample.WebService/HangfireExample.WebService.csproj new file mode 100644 index 0000000..e5635c9 --- /dev/null +++ b/HangfireExample.WebService/HangfireExample.WebService.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/HangfireExample.WebService/HangfireExample.db b/HangfireExample.WebService/HangfireExample.db new file mode 100644 index 0000000000000000000000000000000000000000..237a782f1c297fa002ead7716715fce294d51556 GIT binary patch literal 159744 zcmeI(&2Jp#9l&vqUuNv}TgYM_jBz~K#3ds`5<>&3R0Jk*u&GJoyznBBR^#=9vyHtA zv$HM{QqdYwiDN7NhxStSfO-WXdgZ{SQg2A8*WM~2dg(K-JF~N!%F{^gsNY1+?mX}F z`91UanVrqe?X9{i&3Bs4f$f^l44oJn9v*tpG>3+UhSkp#>L>cMq~4512kKQzydU&- zcm6K^u^JyM?M|7KB5nQrhTJ*sGS}9oBDhFKhK}h#rBFeRB!Cc_gnAnt23_J zZMW$kSFhAtu2bJ>yK;A{S-V?Z?MXQM=4oAA5ZVWR@y$1?H(gsbu>StLbw{qb{!!)f zjrFx#>*nUQE9-BVl}w?E*=(4Vpy;_uRJCGVH+5kNEf`5$>y_rt=B{7(^niB#66r>R zB&$MTnr!K3@}6pEv2Qyn zFWHKBXmzXwLzK(f(NBU9N>^2XyeE9>4Dtof?=gy06q1{@rlSJ*&VEy^0xBBe=|#n! zzVxgg(QGg}rFlcl&ug7#I9BISIya^F?_<3q_7dYkM@{=9NKU)tL>n|Iw(OR>A#KOq zQ8Ou?Mb^q=x_DuJ&~+Y+s=6AyF4P}2#B+;UXEF+vn$_EF*_PF-(yiU`=4X^Lp9x)* z(=8iBF-ljnD{iG?y)D!Nve;cfqEc1Azv$@E!M*t9GCc&TR%iJULo6<8N5K^zH5a{# zbQq%KI1tfQoQ%P7F8y$9Q9LgN*Le5rWcr7rGYJZy+ds$ZjTzuwA~>~rq^eFh7ix0oj@ss8*7>!7SAmFg=wsp3&p zR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Dty10E|kZz{`>S`uwMBNpSd-u}b_y12C ze;=Ct&Fu2bA7;j;e>eTpsXtB~P5x!_x04&Cf0W7-UrxL;{@J*z3h@U41Q0*~0R##t z&>1X!;SM+T`FC;~91lLal?XoH4!DwAJ1;%j zsdhh@#5aJ6b`FtU`Sk%Y+>H-P&{r`7Q5RUbseWx^U{LegYs6VPc?Bxl0^k2H;5LpSBY*$`2q1s}0tg_000Iaga7=-J0psC)y8r+H literal 0 HcmV?d00001 diff --git a/HangfireExample.WebService/Program.cs b/HangfireExample.WebService/Program.cs new file mode 100644 index 0000000..def69f6 --- /dev/null +++ b/HangfireExample.WebService/Program.cs @@ -0,0 +1,29 @@ +using HangfireExample.WebService.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddHangfire(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.MapControllers(); + +app.UseHangfireDashboard(builder.Configuration); + +app.Run(); diff --git a/HangfireExample.WebService/Properties/launchSettings.json b/HangfireExample.WebService/Properties/launchSettings.json new file mode 100644 index 0000000..5c3f504 --- /dev/null +++ b/HangfireExample.WebService/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:1922", + "sslPort": 0 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5185", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/HangfireExample.WebService/appsettings.Development.json b/HangfireExample.WebService/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/HangfireExample.WebService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/HangfireExample.WebService/appsettings.json b/HangfireExample.WebService/appsettings.json new file mode 100644 index 0000000..e1258a6 --- /dev/null +++ b/HangfireExample.WebService/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "DashboardRoot": "/hangfire" +} diff --git a/HangfireExample.sln b/HangfireExample.sln index 9805579..0bf0005 100644 --- a/HangfireExample.sln +++ b/HangfireExample.sln @@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.9.34607.119 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangfireExample", "HangfireExample\HangfireExample.csproj", "{5FF0CD69-D107-4F03-96EF-876BE9D5DA23}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HangfireExample", "HangfireExample\HangfireExample.csproj", "{5FF0CD69-D107-4F03-96EF-876BE9D5DA23}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "方案項目", "方案項目", "{A3B14127-2240-45B0-9441-386F1812E004}" ProjectSection(SolutionItems) = preProject README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangfireExample.WebService", "HangfireExample.WebService\HangfireExample.WebService.csproj", "{EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -20,6 +22,10 @@ Global {5FF0CD69-D107-4F03-96EF-876BE9D5DA23}.Debug|Any CPU.Build.0 = Debug|Any CPU {5FF0CD69-D107-4F03-96EF-876BE9D5DA23}.Release|Any CPU.ActiveCfg = Release|Any CPU {5FF0CD69-D107-4F03-96EF-876BE9D5DA23}.Release|Any CPU.Build.0 = Release|Any CPU + {EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HangfireExample/Program.cs b/HangfireExample/Program.cs index 7cde91c..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 @@ -7,5 +6,10 @@ GlobalConfiguration .Configuration .UseSQLiteStorage("HangfireExample.db"); +// Client 加入一個排程任務 +BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!")); +// Server 啟動 Hangfire 執行排程任務 +using var server = new BackgroundJobServer(); +Console.ReadKey(); \ No newline at end of file diff --git a/README.md b/README.md index 7fb5e23..3f891f1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Getting Started ## Storage + Hangfire 會將所有相關資訊會被序列化後保存到儲存區,如類型、方法或參數等。 儲存模型官方預設有以下幾種 @@ -46,7 +47,7 @@ Hangfire 會將所有相關資訊會被序列化後保存到儲存區,如類 資料來源日期: 2024/03/12 ``` -// Version 1.8 +// Hangfire 1.8 GlobalConfiguration .Configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) @@ -54,7 +55,7 @@ GlobalConfiguration .UseRecommendedSerializerSettings() .UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;"); -// Version 1.7 +// Hangfire 1.7 GlobalConfiguration .Configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)