EaCloud.Hangfire 9.0.0.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package EaCloud.Hangfire --version 9.0.0.8
                    
NuGet\Install-Package EaCloud.Hangfire -Version 9.0.0.8
                    
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="EaCloud.Hangfire" Version="9.0.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EaCloud.Hangfire" Version="9.0.0.8" />
                    
Directory.Packages.props
<PackageReference Include="EaCloud.Hangfire" />
                    
Project file
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 EaCloud.Hangfire --version 9.0.0.8
                    
#r "nuget: EaCloud.Hangfire, 9.0.0.8"
                    
#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 EaCloud.Hangfire@9.0.0.8
                    
#: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=EaCloud.Hangfire&version=9.0.0.8
                    
Install as a Cake Addin
#tool nuget:?package=EaCloud.Hangfire&version=9.0.0.8
                    
Install as a Cake Tool

EaCloud Hangfire 后台任务组件

Member project of EaCloud Team NuGet Badge GitHub license

说明

EaCloud Hangfire 后台任务组件,封装基于 Hangfire 后台任务的服务端实现。

用法

可按照如下配置方式使用:

  1. 通过nuget引用 EaCloud.Hangfire 程序集
Install-Package EaCloud.Hangfire
  1. appsettings.json 中 的 EaCloud 节点下添加如下配置节点
{
   "Hangfire": {
     "Title": "EaCloud任务管理", //显示在仪表板上的标题
     "ServerName": "EaCloudHangfire", //服务器名称
     "WorkerCount": 20, //并发任务数  --超出并发数。将等待之前任务的完成  (推荐并发线程是cpu 的 5倍)
     "StorageType": "Sqlserver", //存储类型:"Sqlserver"、"PostgreSql"、"SQLite"、"MySql"、"Oracle"、"Mongo"、"Redis"、"Memory"
     //存储连接字符串:指定的数据库名必须已经存在,可以手动创建空数据库或者使用当前数据库
     "StorageConnectionString": "Data Source=127.0.0.1;Initial Catalog=EaCloudDEV.Hangfire;User ID=sa;Password=p@ssw0rd;Pooling=true;Min Pool Size=1;Max Pool Size=60;Integrated Security=false;Connect Timeout=60;Application Name=EaCloud",
     "DashboardUrl": "/hangfire", //仪表盘URL路径
     "AuthorizationType": "Basic", //授权访问类型:"Anonymous"、"Basic"
     //Hangfire基本身份验证的选项,当 AuthorizationType = "Basic" 时生效。
     "BasicOptions": {
       "SslRedirect": false, //SSL重定向
       "RequireSsl": false, //需要SSL连接才能访问Hangfire Dashboard
       "LoginCaseSensitive": false, //登录检查是否区分大小写
       //访问Hangfire仪表板的用户列表
       "Users": [
         {
           //登录名
           "UserId": "用户身份标识",
           //登录密码
           "Password": "访问密码"
         }
       ]
     },
     "Enabled": true //是否启用
   } 
}
  1. 若存储类型设为数据库类型(SqlserverPostgreSqlSQLiteMySqlOracleMongo),则需要按照连接串去新建Hangfire用的空数据库(只要库就可以,表会自动生成)。
  2. 启动web项目后,访问http(s)://ip:port/(DashboardUrl)即可打开Hangfire仪表盘界面。
  3. 后端使用Hangfire代码示例 5.1. 项目中新建XXXHangfireJobRunner实现类并添加[Dependency(ServiceLifetime.Singleton)]单例服务特性,继承IHangfireJobRunner接口在Start方法中添加Hangfire任务逻辑
#region "Hangfire作业运行器"
/// <summary>
/// Hangfire作业运行器
/// </summary>
[Dependency(ServiceLifetime.Singleton)]
public class HangfireJobRunner : IHangfireJobRunner
{
    /// <summary>
    /// 启动作业运行器
    /// </summary>
    public void Start()
    {
        //队列任务
        BackgroundJob.Enqueue(() => Console.WriteLine($@"队列任务"));

        //延时任务
        var jobId = BackgroundJob.Schedule(() => Console.WriteLine($@"延时任务"), TimeSpan.FromMinutes(1));

        //延续性任务执行
        BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine($@"延续性任务执行"));

        //定时任务
        RecurringJob.AddOrUpdate("JobId1", () => Console.WriteLine($@"定时任务:每年的4月12号15点52分任意秒执行 没加时区 小时+8"), "* 52 15 12 4 *");   //每年的4月12号15点52分任意秒执行 没加时区 小时+8
        RecurringJob.AddOrUpdate("JobId2", () => Console.WriteLine($@"定时任务:每分钟执行一次"), Cron.Minutely);      //每分钟执行一次。
        RecurringJob.AddOrUpdate("JobId3", () => Console.WriteLine($@"定时任务:每天18点36分 当前时区"), Cron.Daily(18, 36), new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });  //每天18点36分 当前时区

        //本机示例
        RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_Test", m => m.Test(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
        RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_TestAsync", m => m.TestAsync(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
    }
}
#endregion

5.2. 若需要使用注入模式使用,参考上述代码本机示例新建XXXHangfireJob,定义如下结构代码

#region "测试Hangfire作业"
/// <summary>
/// 测试Hangfire作业
/// </summary>
public class XXXHangfireJob
{
    #region "字段"
    #region "私有只读-身份认证上下文"
    /// <summary>
    /// 私有只读-身份认证上下文
    /// </summary>
    private readonly IIdentityContract _identityContract;
    #endregion
    #region "私有只读-服务提供者"
    /// <summary>
    /// 私有只读-服务提供者
    /// </summary>
    private readonly IServiceProvider _provider;
    #endregion

    #endregion

    #region "属性"
    #region "获取 日志对象"
    /// <summary>
    /// 获取 日志对象
    /// </summary>
    protected ILogger Logger => _provider.GetLogger(GetType());
    #endregion

    #endregion

    #region "构造函数"
    #region "初始化一个测试Hangfire作业的新实例"
    /// <summary>
    /// 初始化一个测试Hangfire作业 <see cref="XXXHangfireJob"/> 的新实例
    /// </summary>
    /// <param name="identityContract">身份认证上下文</param>
    /// <param name="provider">服务提供者</param>
    public XXXHangfireJob(IIdentityContract identityContract, IServiceProvider provider)
    {
        _identityContract = identityContract;
        _provider = provider;
    }
    #endregion

    #endregion

    #region "方法"
    #region "同步测试方法"
    /// <summary>
    /// 同步测试方法
    /// </summary>
    /// <returns></returns>
    public void Test()
    {
        //TODO:Your Coding...
        Logger.LogDebug(I18N.T("HangfireTest", "Hangfire Test:{0}", false, DateTime.Now.ToString()));
    }
    #endregion
    #region "异步测试方法"
    /// <summary>
    /// 异步测试方法
    /// </summary>
    /// <returns></returns>
    public async Task TestAsync()
    {
        //TODO:Your Coding...
        Logger.LogDebug(I18N.T("HangfireTestAsync", "Hangfire TestAsync:{0}", false, DateTime.Now.ToString()));

        await Task.CompletedTask;
    }
    #endregion

    #endregion

}
    #endregion
  1. 要禁用Hangfire,设置Enabled: false

交流

QQ WeChat
QQ群号:863605868 微信号:SeonHu
Product 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.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EaCloud.Hangfire:

Package Downloads
EaCloud.Pack.Message

EaCloud 消息模块,封装公告讯息收发及记录管理功能。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.0.9 248 8/12/2025
9.0.0.8 162 7/5/2025
9.0.0.7 248 6/25/2025
9.0.0.6 236 6/18/2025
9.0.0.5 253 6/14/2025
9.0.0.4 282 5/8/2025
9.0.0.3 280 4/23/2025
9.0.0.2 277 4/11/2025
9.0.0.1 252 3/21/2025
8.0.1.9 235 11/15/2024
8.0.1.8 237 11/13/2024
8.0.1.7 260 10/9/2024
8.0.1.6 252 9/29/2024
8.0.1.5 255 9/13/2024
8.0.1.4 290 8/10/2024
8.0.1.3 261 8/7/2024
8.0.1.2 282 8/7/2024
8.0.1.1 246 8/7/2024
8.0.0.9 218 8/7/2024
8.0.0.8 195 8/5/2024
8.0.0.7 204 8/2/2024
8.0.0.6 219 8/1/2024
8.0.0.5 209 8/1/2024
8.0.0.4 191 7/31/2024
8.0.0.3 215 7/30/2024
8.0.0.2 254 7/25/2024
8.0.0.1 240 7/24/2024
7.0.9.4 263 7/19/2024
7.0.9.3 292 7/11/2024
7.0.9.2 290 7/6/2024
7.0.9.1 294 7/3/2024
7.0.8.9 277 6/28/2024
7.0.8.8 294 6/26/2024
7.0.8.7 296 6/22/2024
7.0.8.6 304 6/14/2024
7.0.8.5 225 5/31/2024
7.0.8.4 294 5/21/2024
7.0.8.3 298 5/21/2024
7.0.8.2 246 5/20/2024
7.0.8.1 300 5/20/2024
7.0.7.9 283 5/15/2024
7.0.7.8 262 5/10/2024
7.0.7.7 285 5/9/2024
7.0.7.6 267 5/9/2024
7.0.7.5 318 5/7/2024
7.0.7.4 293 4/28/2024
7.0.7.3 286 4/26/2024
7.0.7.2 299 4/21/2024
7.0.7.1 249 4/19/2024
7.0.6.9 295 4/15/2024
7.0.6.8 294 4/11/2024
7.0.6.7 242 4/11/2024
7.0.6.6 289 4/7/2024
7.0.6.5 289 4/7/2024
7.0.6.4 288 4/7/2024
7.0.6.3 300 4/7/2024
7.0.6.2 294 4/3/2024
7.0.6.1 306 4/3/2024
7.0.5.9 274 3/27/2024
7.0.5.8 303 3/23/2024
7.0.5.7 319 3/17/2024
7.0.5.6 295 3/17/2024
7.0.5.5 312 3/16/2024
7.0.5.4 324 3/13/2024
7.0.5.3 284 3/13/2024
7.0.5.2 307 3/6/2024
7.0.5.1 315 2/21/2024
7.0.4.9 300 2/21/2024
7.0.4.8 327 2/18/2024
7.0.4.7 292 2/5/2024
7.0.4.6 296 2/1/2024
7.0.4.5 319 1/26/2024
7.0.4.4 311 1/22/2024
7.0.4.3 319 1/17/2024
7.0.4.2 248 1/16/2024
7.0.4.1 295 1/16/2024
7.0.3.9 299 1/15/2024
7.0.3.8 311 1/3/2024
7.0.3.7 318 12/28/2023
7.0.3.6 287 12/27/2023
7.0.3.5 327 12/22/2023
7.0.3.4 308 12/22/2023
7.0.3.3 327 12/13/2023
7.0.3.2 308 12/13/2023
7.0.3.1 351 12/12/2023
7.0.2.9 311 11/26/2023
7.0.2.8 305 11/26/2023
7.0.2.7 311 9/26/2023
7.0.2.6 308 9/25/2023
7.0.2.5 302 9/15/2023
7.0.2.4 335 7/27/2023
7.0.2.3 357 7/22/2023
7.0.2.2 374 7/22/2023
7.0.2.1 351 7/21/2023
7.0.1.9 345 7/21/2023
7.0.1.8 355 7/20/2023
7.0.1.7 329 7/20/2023
7.0.1.6 408 7/4/2023
7.0.1.5 371 6/16/2023
7.0.1.4 383 6/13/2023
7.0.1.3 416 6/8/2023
7.0.1.2 388 6/2/2023
7.0.1.1 401 5/11/2023
7.0.0.9 410 5/10/2023
7.0.0.8 396 5/10/2023
7.0.0.7 473 4/19/2023
7.0.0.6 452 4/18/2023
7.0.0.5 521 3/27/2023
7.0.0.4 503 3/23/2023
7.0.0.3 555 1/31/2023
7.0.0.2 552 1/30/2023
7.0.0.1 522 12/24/2022
6.0.2.5 676 11/16/2022
6.0.2.4 674 11/10/2022
6.0.2.3 826 10/11/2022
6.0.2.2 868 9/23/2022
6.0.2.1 924 8/14/2022
6.0.1.9 926 8/13/2022
6.0.1.8 976 6/6/2022
6.0.1.7 945 5/26/2022
6.0.1.6 969 5/11/2022
6.0.1.5 1,018 5/10/2022
6.0.1.4 1,103 4/8/2022
6.0.1.3 1,137 4/2/2022
6.0.1.2 1,140 4/2/2022
6.0.1.1 1,166 3/18/2022
6.0.0.9 1,112 3/5/2022
6.0.0.8 1,132 2/19/2022
6.0.0.7 1,129 2/15/2022
6.0.0.6 1,138 2/14/2022
6.0.0.5 1,241 1/28/2022
6.0.0.4 1,306 1/13/2022
6.0.0.3 1,268 1/12/2022
6.0.0.2 1,529 12/21/2021
6.0.0.1 1,702 12/13/2021
5.0.3.7 2,122 10/16/2021
5.0.3.6 1,228 10/14/2021
5.0.3.5 1,213 10/13/2021
5.0.3.4 1,369 10/8/2021
5.0.3.3 1,319 9/17/2021
5.0.3.2 1,440 9/16/2021
5.0.3.1 1,344 9/15/2021
5.0.2.9 1,351 9/15/2021
5.0.2.8 1,497 8/31/2021
5.0.2.7 1,480 8/28/2021
5.0.2.6 1,371 8/22/2021
5.0.2.5 1,324 8/13/2021
5.0.2.4 1,297 8/13/2021
5.0.2.3 1,223 7/8/2021
5.0.2.2 1,195 7/1/2021
5.0.2.1 1,268 6/16/2021
5.0.1.9 1,187 5/12/2021
5.0.1.8 1,107 5/9/2021
5.0.1.7 1,233 5/7/2021
5.0.1.6 1,263 4/17/2021
5.0.1.5 1,152 4/16/2021
5.0.1.4 1,156 4/15/2021
5.0.1.3 1,161 4/15/2021
5.0.1.2 1,223 4/12/2021