在ASP .NET CORE中使用 hangfire 分布式任务框架

AspNetCore.Hangfire.Extension

hangfire extension

Add Hangfire

 1  services.AddHangfire(config =>
 2             {
 3                 config.UseRedisStorage(
 4                     Configuration["RedisConnectionString"],
 5                     new RedisStorageOptions
 6                     {
 7                         Db = 7,
 8                         Prefix = "abc-sys"
 9                     });
10             });

Use Hangfire

    app.UseHangfireServer();
            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                Authorization = new[] { new HangfireAuthorizationFilter() },
                IgnoreAntiforgeryToken = true,
                AppPath = "/swagger/index.html",
                DashboardTitle = "Abc Sys Hangfire Dashboard"
            });
            app.AddOrUpdateJobs();

HangfireAuthorizationFilter

  /// <summary>
    /// HangfireAuthorizationFilter
    /// </summary>
    public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
    {
        /// <summary>
        /// no authorize
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public bool Authorize(DashboardContext context)
        {
            return true;
        }
    }

TestJob

  /// <summary>
    /// TestJob
    /// </summary>
    [SimpleJob(IsOpen = true, JobId = "TestJob", CronExpression = "0 0 8 * * ?")]
    public class TestJob : BaseRecurringJob
    {
        /// <summary>
        /// execute job
        /// </summary>
        /// <returns></returns>
        public override async Task Execute()
        {
            //todo job
        }
    }

 github url:https://github.com/cailin0630/AspNetCore.Hangfire.Extension

原文地址:https://www.cnblogs.com/jiangyihz/p/12878020.html