113/03/12
This commit is contained in:
31
HangfireExample.WebService/Extensions/HangfireExtension.cs
Normal file
31
HangfireExample.WebService/Extensions/HangfireExtension.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Hangfire;
|
||||||
|
using Hangfire.Storage.SQLite;
|
||||||
|
|
||||||
|
namespace HangfireExample.WebService.Extensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Hangfire 的擴充方法。
|
||||||
|
/// </summary>
|
||||||
|
public static class HangfireExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加入 Hangfire 服務。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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<string>("DashboardRoot"));
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
HangfireExample.WebService/HangfireExample.WebService.csproj
Normal file
19
HangfireExample.WebService/HangfireExample.WebService.csproj
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Hangfire" Version="1.8.11" />
|
||||||
|
<PackageReference Include="Hangfire.Storage.SQLite" Version="0.4.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Controllers\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
BIN
HangfireExample.WebService/HangfireExample.db
Normal file
BIN
HangfireExample.WebService/HangfireExample.db
Normal file
Binary file not shown.
29
HangfireExample.WebService/Program.cs
Normal file
29
HangfireExample.WebService/Program.cs
Normal file
@@ -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();
|
||||||
31
HangfireExample.WebService/Properties/launchSettings.json
Normal file
31
HangfireExample.WebService/Properties/launchSettings.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
HangfireExample.WebService/appsettings.Development.json
Normal file
8
HangfireExample.WebService/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
HangfireExample.WebService/appsettings.json
Normal file
10
HangfireExample.WebService/appsettings.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"DashboardRoot": "/hangfire"
|
||||||
|
}
|
||||||
@@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.9.34607.119
|
VisualStudioVersion = 17.9.34607.119
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
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
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "方案項目", "方案項目", "{A3B14127-2240-45B0-9441-386F1812E004}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "方案項目", "方案項目", "{A3B14127-2240-45B0-9441-386F1812E004}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
README.md = README.md
|
README.md = README.md
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangfireExample.WebService", "HangfireExample.WebService\HangfireExample.WebService.csproj", "{EAEFE8DF-8A3D-4909-9A7C-85E9C6D8555C}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{5FF0CD69-D107-4F03-96EF-876BE9D5DA23}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Hangfire;
|
using Hangfire;
|
||||||
using Hangfire.SqlServer;
|
|
||||||
using Hangfire.Storage.SQLite;
|
using Hangfire.Storage.SQLite;
|
||||||
|
|
||||||
// 第三方擴充: Hangfire.Storage.SQLite 0.4.1
|
// 第三方擴充: Hangfire.Storage.SQLite 0.4.1
|
||||||
@@ -7,5 +6,10 @@ GlobalConfiguration
|
|||||||
.Configuration
|
.Configuration
|
||||||
.UseSQLiteStorage("HangfireExample.db");
|
.UseSQLiteStorage("HangfireExample.db");
|
||||||
|
|
||||||
|
// Client 加入一個排程任務
|
||||||
|
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
|
||||||
|
|
||||||
|
// Server 啟動 Hangfire 執行排程任務
|
||||||
|
using var server = new BackgroundJobServer();
|
||||||
|
|
||||||
|
Console.ReadKey();
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
# Getting Started
|
# Getting Started
|
||||||
|
|
||||||
## Storage
|
## Storage
|
||||||
|
|
||||||
Hangfire 會將所有相關資訊會被序列化後保存到儲存區,如類型、方法或參數等。
|
Hangfire 會將所有相關資訊會被序列化後保存到儲存區,如類型、方法或參數等。
|
||||||
|
|
||||||
儲存模型官方預設有以下幾種
|
儲存模型官方預設有以下幾種
|
||||||
@@ -46,7 +47,7 @@ Hangfire 會將所有相關資訊會被序列化後保存到儲存區,如類
|
|||||||
資料來源日期: 2024/03/12
|
資料來源日期: 2024/03/12
|
||||||
|
|
||||||
```
|
```
|
||||||
// Version 1.8
|
// Hangfire 1.8
|
||||||
GlobalConfiguration
|
GlobalConfiguration
|
||||||
.Configuration
|
.Configuration
|
||||||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
||||||
@@ -54,7 +55,7 @@ GlobalConfiguration
|
|||||||
.UseRecommendedSerializerSettings()
|
.UseRecommendedSerializerSettings()
|
||||||
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;");
|
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;");
|
||||||
|
|
||||||
// Version 1.7
|
// Hangfire 1.7
|
||||||
GlobalConfiguration
|
GlobalConfiguration
|
||||||
.Configuration
|
.Configuration
|
||||||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
|
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
|
||||||
|
|||||||
Reference in New Issue
Block a user