You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.9 KiB
C#

2 months ago
using System.Linq.Expressions;
using Hangfire;
using Hangfire.Dashboard.BasicAuthorization;
using Microsoft.AspNetCore.Builder;
namespace DS.WMS.Core.HangfireJob.Method
{
/// <summary>
/// 注册Hangfire定时任务的中间件
/// </summary>
public static class JobMiddleware
{
/// <summary>
/// 注册Hangfire定时任务的中间件
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static WebApplication UseJobMiddlewares(this WebApplication app)
{
//app.UseHangfireServer(); // 用于将 Hangfire 任务处理服务器添加到请求处理管道中
// 将 Hangfire 仪表板添加到应用程序的请求处理管道中
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
2 months ago
Authorization = [new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
{
RequireSsl = false,
SslRedirect = false,
LoginCaseSensitive = true,
Users = new []
{
new BasicAuthAuthorizationUser
{
Login = "admin",
PasswordClear = "ds2024"
}
}
2 months ago
})]
});
return app;
}
2 months ago
/// <summary>
/// 注册定时任务
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="methodCall"></param>
/// <param name="cron">CRON表达式</param>
/// <param name="jobId">任务ID</param>
public static void RegisterJob<T>(Expression<Action<T>> methodCall, string cron, string? jobId = null)
{
RecurringJob.AddOrUpdate(jobId ?? typeof(T).FullName, methodCall, cron);
}
}
}