using Hangfire; using Microsoft.Extensions.DependencyInjection; using System; namespace Kean.Infrastructure.Hangfire { /// /// ServiceCollection 扩展方法 /// public static class ServiceCollectionExtensions { /// /// 向服务描述中追加 Hangfire 配置 /// /// 服务描述符 /// 配置项 /// 服务描述符 public static IServiceCollection AddHangfire(this IServiceCollection services, Action setupAction) { var options = new HangfireOptions(); setupAction.Invoke(options); return services .AddHangfire(configuration => { configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseRedisStorage(options.RedisStorage.ConnectionString, new() { Db = options.RedisStorage.Database }); }) .AddHangfireServer(options => options.SchedulePollingInterval = TimeSpan.FromSeconds(1)) .AddHostedService(serviceProvider => new HostedService(options.RecurringJobs)); } } }