net6 hangfire 定时作业+aop+登录验证 vs2022


直接拿去用    

Program 中先注册

使用HttpJon  Nuget引入
//Hangfire  
//Hangfire.AspNetCore 
//Hangfire.Console  
//Hangfire.Dashboard.BasicAuthorization   --可视化+权限控制
//Hangfire.HttpJob                        --httpJonb
//Hangfire.SqlServer                      --数据库
//Hangfire.Tags.SqlServer

var builder = WebApplication.CreateBuilder(args);
{
    builder.Services.AddHangfire(configura =>
    {
        //指定存储介质
        configura.UseSqlServerStorage(builder.Configuration["ConnectionStrings:HangfireConnectionString"], new SqlServerStorageOptions() //Nuget引入:   数据库连接字符串  先创建好数据库
        {
            CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),          //滑动超时
            SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            QueuePollInterval = TimeSpan.Zero,                    //队列轮训间隔
            UseRecommendedIsolationLevel = true,
            UsePageLocksOnDequeue = true,                          //出列时是需要页锁
            DisableGlobalLocks = true                              // 是否禁用全局锁
        }).UseTagsWithSql()                                     //nuget引入Hangfire.Tags.SqlServer
        .UseConsole(new ConsoleOptions()
        {
            BackgroundColor = "#000079"
        }).UseHangfireHttpJob();

    });
}
////后台作业--会保存到数据库中去: 只执行一次
//BackgroundJob.Enqueue(() => Console.WriteLine($"欢迎大家来到,现在时间:{DateTime.Now.ToString()}"));

////间隔一段时间去执行; 间隔一分钟,就去执行一次
//RecurringJob.AddOrUpdate(() => Console.WriteLine($"欢迎大家来到,现在时间:{ DateTime.Now.ToString()}"), Cron.Minutely());

//过了多长时间以后,去执行
//var jobId1 = BackgroundJob.Schedule(() => Console.WriteLine("Delayed!"), TimeSpan.FromMinutes(1));


//如果要执行的任务--代码量很多;--怎么办?

RecurringJob.AddOrUpdate(c => c.RefreshSysUserOnCompanyName(), Cron.Minutely);       //自定义的任务处理


var app = builder.Build();
{
    app.UseHangfireServer();//启动HangfireServer
    app.UseHangfireDashboard("/hangfire", new DashboardOptions()
    {
        //这里是默认实现,如果使用这个,小心坑:nuget引入:Hangfire.Dashboard.BasicAuthorization
        //Authorization = new BasicAuthAuthorizationFilter[] {
        //      new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions(){
        //            SslRedirect=false,
        //            RequireSsl=false,
        //            Users=new BasicAuthAuthorizationUser[]{
        //                  new BasicAuthAuthorizationUser(){
        //                      Login="admin",
        //                      PasswordClear="test",

        //                  }
        //             }

        //      })
        //}

        Authorization = new CustomDashboardAuthorizationFilter[] {
            new CustomDashboardAuthorizationFilter()   //自定义的filter
        }

    }); ;
}
 public class SynchronousDataService: ISynchronousDataService
    {
        public void RefreshSysUserOnCompanyName()
        {
            Console.WriteLine($"刷新数据库的---SysUser-CompanyName--{DateTime.Now.ToString()}");
        }
    }
public class CustomDashboardAuthorizationFilter : IDashboardAuthorizationFilter
    {
        /// 
        /// 在这里可以调用到HttpContext,就可以根据上下文中的用户信息来判断当前用户是否可以访问这个控制版面   可以JWT 
        /// 
        /// 
        /// 
        public bool Authorize([NotNull] DashboardContext context)
        {
            Console.WriteLine("111111111111111");
            return true;
        }
    }