Tenon.Hangfire.Extensions
0.0.1-alpha-202502101554
This is a prerelease version of Tenon.Hangfire.Extensions.
dotnet add package Tenon.Hangfire.Extensions --version 0.0.1-alpha-202502101554
NuGet\Install-Package Tenon.Hangfire.Extensions -Version 0.0.1-alpha-202502101554
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tenon.Hangfire.Extensions" Version="0.0.1-alpha-202502101554" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.Hangfire.Extensions --version 0.0.1-alpha-202502101554
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Tenon.Hangfire.Extensions, 0.0.1-alpha-202502101554"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tenon.Hangfire.Extensions as a Cake Addin #addin nuget:?package=Tenon.Hangfire.Extensions&version=0.0.1-alpha-202502101554&prerelease // Install Tenon.Hangfire.Extensions as a Cake Tool #tool nuget:?package=Tenon.Hangfire.Extensions&version=0.0.1-alpha-202502101554&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Tenon.Hangfire.Extensions
Hangfire 扩展包,提供了更灵活的配置选项和额外的功能特性。
功能特性
- IP 白名单授权
- 基本认证支持
- 灵活的配置选项
- 内置缓存支持
- 环境感知配置
- 日志记录增强
示例项目
完整的使用示例请参考 HangfireSample 项目,其中包含:
- SQLite 存储配置示例
- 多种服务器配置方式
- 完整的安全认证配置
- 各类任务示例代码
- 性能优化建议
快速开始
- 安装 NuGet 包:
dotnet add package Tenon.Hangfire.Extensions
- 注册服务:
// 注册 Hangfire 缓存提供程序
services.AddSingleton<IHangfireCacheProvider, HangfireMemoryCacheProvider>();
// 添加 Hangfire 服务
services.AddHangfireServices(
configuration.GetSection("Hangfire"),
configureStorage: config =>
{
// 配置存储
},
configureServer: options =>
{
// 配置服务器选项
});
- 配置中间件:
app.UseHangfire();
配置说明
appsettings.json 配置示例
{
"Hangfire": {
"Path": "/hangfire",
"DashboardTitle": "任务调度中心",
"IgnoreAntiforgeryToken": true,
"SkipBasicAuthenticationIfIpAuthorized": true,
"Server": {
"WorkerCount": 10,
"Queues": [ "critical", "default", "low" ],
"ServerTimeoutMinutes": 5,
"ShutdownTimeoutMinutes": 2,
"ServerName": "CustomHangfireServer"
},
"IpAuthorization": {
"Enabled": true,
"AllowedIPs": [ "127.0.0.1", "::1" ],
"AllowedIpRanges": [ "192.168.1.0/24" ]
},
"Authentication": {
"Username": "admin",
"Password": "123456",
"MaxFailedAttempts": 3,
"LockoutTime": "00:05:00"
}
}
}
服务器配置方式
- 环境感知配置:
if (environment.IsDevelopment())
{
options.WorkerCount = 5;
options.Queues = new[] { "development", "default" };
}
- 配置文件配置:
var serverSection = configuration.GetSection("Hangfire:Server");
if (serverSection.Exists())
{
var workerCount = serverSection.GetValue<int?>("WorkerCount");
if (workerCount.HasValue)
{
options.WorkerCount = workerCount.Value;
}
}
- 环境变量配置:
var envWorkerCount = Environment.GetEnvironmentVariable("HANGFIRE_WORKER_COUNT");
if (!string.IsNullOrEmpty(envWorkerCount) && int.TryParse(envWorkerCount, out var count))
{
options.WorkerCount = count;
}
- 动态计算配置:
options.WorkerCount = Math.Min(
Environment.ProcessorCount * 5,
Math.Max(5, Environment.ProcessorCount * 2)
);
安全特性
IP 授权
- 支持具体 IP 地址白名单
- 支持 CIDR 格式的 IP 范围
- 可选择性跳过基本认证
基本认证
- 用户名密码认证
- 登录失败次数限制
- 账户锁定机制
- 密码复杂度验证
日志记录
内置详细的日志记录:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Hangfire": "Information",
"Tenon.Hangfire.Extensions.Filters": "Debug"
}
}
}
最佳实践
环境配置:
- 开发环境使用较少的工作线程
- 生产环境根据负载调整线程数
- 不同环境使用不同的队列优先级
安全配置:
- 生产环境禁用 IgnoreAntiforgeryToken
- 使用复杂的密码
- 配置适当的 IP 白名单
性能优化:
- 根据 CPU 核心数配置工作线程
- 合理设置队列优先级
- 适当配置超时时间
许可证
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Hangfire.AspNetCore (>= 1.8.17)
- Hangfire.Core (>= 1.8.17)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Text.Encodings.Web (>= 9.0.0)
- Tenon.Caching.Abstractions (>= 0.0.1-alpha-202502101554)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.1-alpha-202502101554 | 35 | 2/10/2025 |
0.0.1-alpha-202502101448 | 31 | 2/10/2025 |
0.0.1-alpha-202502101434 | 36 | 2/10/2025 |
0.0.1-alpha-202501130258 | 35 | 1/13/2025 |