using Medallion.Threading; using Medallion.Threading.Redis; using Microsoft.AspNetCore.SignalR.StackExchangeRedis; using Volo.Abp.BlobStoring; #pragma warning disable CS0618 // Type or member is obsolete namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions { /// /// 注册Redis缓存 /// public static IServiceCollection AddAbpProRedis(this IServiceCollection service, Action configureOptions = null) { var configuration = service.GetConfiguration(); var redisEnabled = configuration["Redis:IsEnabled"]; if (!string.IsNullOrEmpty(redisEnabled) && !bool.Parse(redisEnabled)) return service; if (configureOptions != null) { service.Configure(configureOptions); } else { service.Configure(options => { options.KeyPrefix = "AbpPro:"; }); } var redis = ConnectionMultiplexer.Connect(configuration.GetValue("Redis:Configuration")); service.AddDataProtection().PersistKeysToStackExchangeRedis(redis, "AbpPro-Protection-Keys"); return service; } /// /// 注册redis分布式锁 /// public static IServiceCollection AddAbpProRedisDistributedLocking(this IServiceCollection service) { var configuration = service.GetConfiguration(); var redisEnabled = configuration["Redis:IsEnabled"]; if (!string.IsNullOrEmpty(redisEnabled) && !bool.Parse(redisEnabled)) return service; var connectionString = configuration.GetValue("Redis:Configuration"); service.AddSingleton(sp => { var connection = ConnectionMultiplexer.Connect(connectionString); return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); }); return service; } /// /// 注册Identity /// public static IServiceCollection AddAbpProIdentity(this IServiceCollection service) { service.Configure(options => { options.Lockout = new LockoutOptions() { AllowedForNewUsers = false }; }); return service; } /// /// 注册SignalR /// public static IServiceCollection AddAbpProSignalR(this IServiceCollection service, Action redisOptions = null) { var configuration = service.GetConfiguration(); var redisEnabled = configuration["Redis:IsEnabled"]; if (!string.IsNullOrEmpty(redisEnabled) && !bool.Parse(redisEnabled)) { if (redisOptions != null) { service .AddSignalR() .AddStackExchangeRedis(service.GetConfiguration().GetValue("Redis:Configuration"), redisOptions); } else { service .AddSignalR() .AddStackExchangeRedis(service.GetConfiguration().GetValue("Redis:Configuration"), options => { options.Configuration.ChannelPrefix = "Lion.AbpPro"; }); } } else { service.AddSignalR(); } return service; } /// /// 注册基于FileSystem的blob设置 /// public static IServiceCollection AddAbpProBlobStorageFileSystem(this IServiceCollection service) { service.Configure(options => { options.Containers.ConfigureDefault(container => { container.UseFileSystem(fileSystem => { fileSystem.BasePath = "C:\\my-files"; }); }); }); return service; } // /// // /// 注册cap // /// // public static IServiceCollection AddAbpProCap(this IServiceCollection service) // { // var configuration = service.GetConfiguration(); // service.AddAbpCap(capOptions => // { // capOptions.SetCapDbConnectionString(configuration["ConnectionStrings:Default"]); // capOptions.UseEntityFramework(); // capOptions.UseRabbitMQ(option => // { // option.HostName = configuration.GetValue("Cap:RabbitMq:HostName"); // option.UserName = configuration.GetValue("Cap:RabbitMq:UserName"); // option.Password = configuration.GetValue("Cap:RabbitMq:Password"); // option.Port = configuration.GetValue("Cap:RabbitMq:Port"); // }); // capOptions.UseDashboard(options => { options.AuthorizationPolicy = AbpProCapPermissions.CapManagement.Cap; }); // }); // return service; // } // /// // /// 注册hangfire // /// // public static IServiceCollection AddAbpProHangfire(this IServiceCollection service) // { // var redisStorageOptions = new RedisStorageOptions() // { // Db = service.GetConfiguration().GetValue("Hangfire:Redis:DB") // }; // // service.Configure(options => { options.IsJobExecutionEnabled = true; }); // // service.AddHangfire(config => // { // config.UseRedisStorage(service.GetConfiguration().GetValue("Hangfire:Redis:Host"), redisStorageOptions) // .WithJobExpirationTimeout(TimeSpan.FromDays(7)); // var delaysInSeconds = new[] { 10, 60, 60 * 3 }; // 重试时间间隔 // const int attempts = 3; // 重试次数 // config.UseFilter(new AutomaticRetryAttribute() { Attempts = 3, DelaysInSeconds = delaysInSeconds }); // //config.UseFilter(new AutoDeleteAfterSuccessAttribute(TimeSpan.FromDays(7))); // //config.UseFilter(new JobRetryLastFilter(attempts)); // }); // return service; // } }