安装包-NuGet

Hangfire.Core
Hangfire.AspNetCore
Hangfire.Dashboard.BasicAuthorization
Hangfire.SqlServer

服务配置

新建一个类 用作service配置

  1. public static class HangfireService
  2. {
  3. public static void AddHangfireService(this IServiceCollection services)
  4. {
  5. // Add Hangfire services.
  6. if (services == null) throw new ArgumentNullException(nameof(services));
  7. services.AddHangfire(configuration => configuration
  8. .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
  9. .UseSimpleAssemblyNameTypeSerializer()
  10. .UseRecommendedSerializerSettings()
  11. .UseSqlServerStorage("数据库连接字符串", new SqlServerStorageOptions
  12. {
  13. CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
  14. SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
  15. QueuePollInterval = TimeSpan.Zero,
  16. UseRecommendedIsolationLevel = true,
  17. UsePageLocksOnDequeue = true,
  18. DisableGlobalLocks = true,
  19. //JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 作业到期检查间隔(管理过期记录)。默认值为1小时。
  20. //CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。
  21. }));
  22. // Add the processing server as IHostedService
  23. services.AddHangfireServer();
  24. }
  25. }

StartUp中添加服务

  1. services.AddHangfireService();

数据面板配置

新建一个类 进行配置

  1. public static class HangfireMiddleware
  2. {
  3. public static void UseHangfireMiddleware(this IApplicationBuilder app)
  4. {
  5. if (app == null) throw new ArgumentNullException(nameof(app));
  6. app.UseHangfireServer(ConfigureOptions());//配置服务
  7. app.UseHangfireDashboard(AppsettingConfig.HangfirePathMatch, HfAuthor());//配置面板
  8. //backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
  9. //backgroundJobs.AddOrUpdate();
  10. HangfireService();//配置各个任务
  11. }
  12. /// <summary>
  13. /// 配置账号模板信息
  14. /// </summary>
  15. /// <returns></returns>
  16. public static DashboardOptions HfAuthor()
  17. {
  18. var filter = new BasicAuthAuthorizationFilter(
  19. new BasicAuthAuthorizationFilterOptions
  20. {
  21. SslRedirect = false,
  22. RequireSsl = false,
  23. LoginCaseSensitive = false,
  24. Users = new[]
  25. {
  26. new BasicAuthAuthorizationUser
  27. {
  28. Login = 账号, //可视化的登陆账号
  29. PasswordClear = 密码 //可视化的密码
  30. }
  31. }
  32. });
  33. return new DashboardOptions
  34. {
  35. Authorization = new[] { filter }
  36. };
  37. }
  38. /// <summary>
  39. /// 配置启动
  40. /// </summary>
  41. /// <returns></returns>
  42. public static BackgroundJobServerOptions ConfigureOptions()
  43. {
  44. return new BackgroundJobServerOptions
  45. {
  46. Queues = new[] { HangfireConfigureQueue.Myqueue, HangfireConfigureQueue.Default },//队列名称,只能为小写
  47. WorkerCount = Environment.ProcessorCount * 5, //并发任务
  48. ServerName = AppsettingConfig.HangfireServerName, //代表服务名称
  49. };
  50. }
  51. #region 配置服务
  52. public static void HangfireService()
  53. {
  54. //这里呢就是需要触发的方法 "0/10 * * * * ? " 可以自行搜索cron表达式 代表循环的规律很简单
  55. //GameLenthPush代表你要触发的类 UserGameLenthPush代表你要触发的方法
  56. //RecurringJob.AddOrUpdate<GameLenthPush>(s => s.UserGameLenthPush(), "0/10 * * * * ? ", TimeZoneInfo.Local);
  57. //RecurringJob.AddOrUpdate<ITestServices>(s => s.TestHangfiredefault(), "0/5 * * * * ? ", TimeZoneInfo.Local);
  58. //RecurringJob.AddOrUpdate<ITestServices>(s => s.TestHangfireMyqueue(), "0/5 * * * * ? ", TimeZoneInfo.Local, HangfireConfigureQueue.Myqueue);
  59. }
  60. #endregion
  61. }

添加服务面板

// 使用Hangfire
app.UseHangfireMiddleware();