Zzz.Hangfire
1.1.1
dotnet add package Zzz.Hangfire --version 1.1.1
NuGet\Install-Package Zzz.Hangfire -Version 1.1.1
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="Zzz.Hangfire" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zzz.Hangfire" Version="1.1.1" />
<PackageReference Include="Zzz.Hangfire" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Zzz.Hangfire --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Zzz.Hangfire, 1.1.1"
#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.
#:package Zzz.Hangfire@1.1.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Zzz.Hangfire&version=1.1.1
#tool nuget:?package=Zzz.Hangfire&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Zzz.Hangfire
说明
对Hangfire的循环任务进行封装,对实现了接口的类动态启动循环任务;
使用
启动
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
...
//注入
builder.Services.AddZHangfire(option =>
{
option.RoutePrefix = "/job";
option.UserName = "admin";
option.Password = "admin";
option.Redis = "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=3";
option.Interval = 5;
});
var app = builder.Build();
...
//开启Hangfire
app.UseZHangfire();
app.UseAuthorization();
app.MapControllers();
app.Run();
创建任务
- 实现
IHangfireJob
:创建一个取消订单的任务,实现接口。 - 设置执行计划:加上
[ExecutePlan("0/5 * * * * *")]
。 - 编码:实现任务功能。
/// <summary>
/// 取消过期订单
/// </summary>
[ExecutePlan("0/5 * * * * *")]
public class CancelExpiredOrderTask : IHangfireJob
{
public Task Execute()
{
Console.WriteLine("取消过期订单"+DateTime.Now);
return Task.CompletedTask;
}
}
指定程序集
如果创建的任务文件不在当前程序集下,比如单独创建一个xxx.Task类库项目,只需要启动参数Assembile
即可加载指定程序集的所有任务,否则加载当前程序集下的所有Task
builder.Services.AddZHangfire(option =>
{
option.RoutePrefix = "/job";
option.UserName = "admin";
option.Password = "admin";
option.Redis = "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=3";
option.Interval = 5;
option.Assembile = "Zzz.ZHangfire.Lib";
});
分组
对任务进行分组,如果Task放在同一个类库项目下,同时又希望多个应用分别执行不同的任务,可以使用分组功能,在执行计划的特性上指定Group
即可
builder.Services.AddZHangfire(option =>
{
option.RoutePrefix = "/job";
option.UserName = "admin";
option.Password = "admin";
option.Redis = "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=3";
option.Interval = 5;
option.Assembile = "Zzz.ZHangfire.Lib";
option.Group= "order";
});
给执行计划指定分组
/// <summary>
/// 测试
/// </summary>
[ExecutePlan("0/5 * * * * *","order")]
public class TestTask : IHangfireJob
{
public Task Execute()
{
Console.WriteLine("Test");
return Task.CompletedTask;
}
}
注意:为防止意外执行任务,Task指定了分组,而配置里为指定分组,会按照配置的默认分组(Default)加载Task; 所以当Task的执行计划(
ExecutePlanAttribute
)中的分组与启动时的配置指定的分组(option.Group
)不一致时,会按照配置上的分组加载
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Hangfire.AspNetCore (>= 1.8.18)
- Hangfire.Core (>= 1.8.18)
- Hangfire.Dashboard.BasicAuthorization (>= 1.0.2)
- Hangfire.Redis.StackExchange (>= 1.12.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- StackExchange.Redis (>= 2.8.31)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.