NET6 Hangfire 可视化配置

Nuget

<PackageReference Include="Hangfire.AspNetCore" Version="1.8.5" />
<PackageReference Include="Hangfire.Dashboard.BasicAuthorization" Version="1.0.2" />
<PackageReference Include="Hangfire.HttpJob" Version="3.8.1" />
<!--MSSQL-->
<PackageReference Include="Hangfire.SqlServer" Version="1.8.5" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<!--SQLite-->
<PackageReference Include="Hangfire.Storage.SQLite" Version="0.4.2" />
<!--<PackageReference Include="Hangfire" Version="1.8.5" />
<PackageReference Include="Hangfire.Console" Version="1.4.2" />
<PackageReference Include="Hangfire.Core" Version="1.8.5" />-->

强制中文

Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");

代码

Program

Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHangfireWithSQLite(builder.Configuration);

var app = builder.Build();
app.UseHangfireDashboard("/hangfire");

app.Run();

Add Method - MSSQL

    /// <summary>
    /// Hangfire 访问 MSSQL
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    public static void AddHangfireWithMssql(this IServiceCollection services, IConfiguration configuration)
    {
        // Add Hangfire services.
        services.AddHangfire(config =>
        {
            // 设置Hangfire的本地化选项:如果没有效果,则强制中文
            config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
            .UseDefaultCulture(new CultureInfo("zh-CN"))
            .UseSimpleAssemblyNameTypeSerializer()  //    序列化器组件
            .UseRecommendedSerializerSettings() //   全局序列化器
            .UseStorage(new SqlServerStorage(configuration["ConnectionStrings:HangfireConnection"], new SqlServerStorageOptions
            {
                QueuePollInterval = TimeSpan.FromSeconds(15),
                JobExpirationCheckInterval = TimeSpan.FromHours(1),
                CountersAggregateInterval = TimeSpan.FromMinutes(5),
                PrepareSchemaIfNecessary = true,
                DashboardJobListLimit = 50000,
                TransactionTimeout = TimeSpan.FromMinutes(1)
                //TablesPrefix = "Hangfire"
            }))
            //  增加按钮,动态维护调度
            .UseHangfireHttpJob()
            ;
        });

        // Add the processing server as IHostedService
        services.AddHangfireServer();
    }

Add Method - SQLite

    /// <summary>
    /// Hangfire 访问 SQLite
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    public static void AddHangfireWithSQLite(this IServiceCollection services, IConfiguration configuration)
    {
        // Add Hangfire services.
        services.AddHangfire(config =>
        {
            // 设置Hangfire的本地化选项:如果没有效果,则强制中文
            config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
            .UseDefaultCulture(new CultureInfo("zh-CN"))
            .UseSimpleAssemblyNameTypeSerializer()  //    序列化器组件
            .UseRecommendedSerializerSettings() //   全局序列化器
            // .UseSQLiteStorage(/*"Data Source=hangfire.db"*/) // 默认在当前目录创建文件:hangfire.db
            .UseStorage(new SQLiteStorage("Outputs/db/hangfire.db", new SQLiteStorageOptions())) // 自定义位置创建DB文件
// 增加按钮,动态维护调度 .UseHangfireHttpJob() ; }); // Add the processing server as IHostedService services.AddHangfireServer(); }

Use Method

    /// <summary>
    /// 使用仪表盘,配置授权
    /// </summary>
    /// <param name="webapp"></param>
    /// <param name="pathMatch"></param>
    public static void UseHangfireDashboardWithCust(this WebApplication webapp, string pathMatch = "/hangfire")
    {
        webapp.UseHangfireDashboard(pathMatch, new DashboardOptions
        {
            Authorization = new[] {
            new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
            {
                RequireSsl = false,
                SslRedirect = false,
                LoginCaseSensitive = true,
                Users = new []
                {
                    new BasicAuthAuthorizationUser
                    {
                        Login = webapp.Configuration.GetSection("HangfireCredentials:UserName").Value,
                        PasswordClear = webapp.Configuration.GetSection("HangfireCredentials:Password").Value
                    }
                }
            })}
        });
    }

Hangfire添加周期性任务的参数说明

RecurringJobIdentifier:周期性任务的唯一标识符。
JobName:任务的名称。
Method:请求的HTTP方法(例如:GET、POST)。
ContentType:请求的内容类型。
Url:要调用的端点的URL。
Headers:要包含在请求中的其他头部信息。
Data:要发送的请求数据。
Timeout:请求的超时时间(以毫秒为单位)。
TimeZone:用于调度周期性任务的时区。
Cron:指定周期性任务调度的cron表达式。
AgentClass:处理任务执行的类。
AgentTimeout:处理任务的代理超时时间。
BasicUserName:基本身份验证的用户名(如果需要)。
BasicPassword:基本身份验证的密码(如果需要)。
QueueName:将任务加入的队列的名称。
EnableRetry:指示是否对失败的任务启用重试。
RetryTimes:失败任务的重试次数。
RetryDelaysInSeconds:重试之间的延迟时间(以秒为单位)。
SendSuccess:指示是否在任务成功时发送通知。
SendFail:指示是否在任务失败时发送通知。
Mail:通知将发送到的电子邮件地址。
CallbackEL:用于接收任务执行状态更新的回调端点。

 

Reference:

周期性任务参数
添加仅执行一次的任务
添加周期性任务
IIS 防止自动回收
HangfireHttpJob
使用仪表盘
posted @ 2024-04-22 17:32  Robot-Blog  阅读(21)  评论(0编辑  收藏  举报