HcfNS.Hangfire
7.0.2.2
dotnet add package HcfNS.Hangfire --version 7.0.2.2
NuGet\Install-Package HcfNS.Hangfire -Version 7.0.2.2
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="HcfNS.Hangfire" Version="7.0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HcfNS.Hangfire --version 7.0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HcfNS.Hangfire, 7.0.2.2"
#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 HcfNS.Hangfire as a Cake Addin #addin nuget:?package=HcfNS.Hangfire&version=7.0.2.2 // Install HcfNS.Hangfire as a Cake Tool #tool nuget:?package=HcfNS.Hangfire&version=7.0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hcf Hangfire 后台任务组件,封装基于Hangfire后台任务的组件包实现
用法
通过nuget引用 Hcf.Hangfire 程序集
Install-Package HcfNS.Hangfire
添加HangfirePack组件包
services.AddPack<HangfirePack>();
配置
"Hcf":{
"Hangfire": {
"WorkerCount": 20,
"ServerName":"xxx",
"Queues":[{"defult"}],
"SchedulePollingInterval":15,//秒
"StorageConnectionString": "Server=.;Database=hcf.admin.dev;User Id=sa;Password=123456;MultipleActiveResultSets=true",
"DashboardUrl": "/hangfire",
"Roles": "",
"Enabled": true
}
}
示例
创建一个HangfireJobRunner实现IHangfireJobRunner接口
public class HangfireJobRunner : IHangfireJobRunner
{
public void Start()
{
//一次性作业
string taskName = "Fire-and-forget tasks";
BackgroundJob.Enqueue(() => Console.WriteLine($"{taskName} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"));
BackgroundJob.Enqueue<TestHangfireJob>(a => a.GetDashboardUrl(taskName, "测试内容"));
//延迟任务
taskName = "Delayed tasks";
string jobId1 = BackgroundJob.Schedule(() => Console.WriteLine($"{taskName} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},我是job1"), TimeSpan.FromSeconds(10));
string jobId2 = BackgroundJob.Schedule<TestHangfireJob>(a => a.GetDashboardUrl(taskName, "我是job2"), TimeSpan.FromSeconds(10));
//工作流任务
taskName = "Continuations tasks";
BackgroundJob.ContinueJobWith(jobId1, () => Console.WriteLine($"{taskName} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} job1后续的任务"));
BackgroundJob.ContinueJobWith<TestHangfireJob>(jobId2, a => a.GetDashboardUrl(taskName, "测试job2后续的任务"));
//循环任务
taskName = "Recurring tasks 1 min";
RecurringJob.AddOrUpdate("test1", () => Console.WriteLine($"{taskName} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 1分钟执行一次"), Cron.Minutely, new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
//RecurringJob.AddOrUpdate<TestHangfireJob>(a => a.GetDashboardUrl(taskName, "1分钟执行一次"), Cron.Minutely, TimeZoneInfo.Local);
RecurringJob.AddOrUpdate<TestHangfireJob>("test2", a => a.GetDashboardUrl(taskName, "test2"), Cron.Minutely, new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
//taskName = "Recurring tasks 1 sec";
//RecurringJob.AddOrUpdate(() => Console.WriteLine($"{taskName} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 20秒执行一次"), "*/20 * * * * *");
//RecurringJob.AddOrUpdate<TestHangfireJob>(a => a.GetDashboardUrl(taskName, "20秒执行一次"), "*/20 * * * * *");
}
}
public class TestHangfireJob
{
private readonly IConfiguration _configuration;
public TestHangfireJob(IConfiguration configuration)
{
_configuration = configuration;
}
[Queue("test")]
public string GetDashboardUrl(string name, string content)
{
string dashboardUrl = _configuration["Hcf:Hangfire:DashboardUrl"].CastTo("/hangfire");
Console.WriteLine($"{name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, dashboardUrl is {dashboardUrl},content is {content}");
return dashboardUrl;
}
public static string GetDashboardUrl1(string name, string content)
{
string dashboardUrl = "/hangfire1";
Console.WriteLine($"{name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, dashboardUrl is {dashboardUrl},content is {content}");
return dashboardUrl;
}
}
#Hangfire 1.7.0+ 支持使用按秒循环任务执行 RecurringJob.AddOrUpdate("test",()=>writeLog("每20秒执行任务"),"*/20 * * * * *");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Hangfire (>= 1.8.17)
- HcfNS.Core (>= 7.0.2.1)
-
net7.0
- Hangfire (>= 1.8.17)
- HcfNS.Core (>= 7.0.2.1)
-
net8.0
- Hangfire (>= 1.8.17)
- HcfNS.Core (>= 7.0.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.