MultipleAzureRedisFactoryExtensions.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.DependencyInjection.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using TEAMModelOS.SDK.DI;
  7. namespace TEAMModelOS.SDK.DI.Multiple
  8. {
  9. public static class MultipleAzureRedisFactoryExtensions
  10. {
  11. /// <summary>
  12. /// Redis数据库注入
  13. /// </summary>
  14. /// <param name="services"></param>
  15. /// <param name="connectionInfos"></param>
  16. /// <returns></returns>
  17. /// <exception cref="ArgumentNullException"></exception>
  18. public static IServiceCollection AddBIAzureRedis(this IServiceCollection services, List<(string name, string connectionString)> connectionInfos)
  19. {
  20. if (services == null) throw new ArgumentNullException(nameof(services));
  21. if (connectionInfos == null) throw new ArgumentNullException(nameof(connectionInfos));
  22. services.TryAddSingleton<AzureRedisFactory>();
  23. //多个连接字符串注入
  24. connectionInfos.ForEach(connection =>
  25. {
  26. services.Configure<AzureRedisFactoryOptions>(connection.name, o => { o.Name = connection.name; o.RedisConnectionString = connection.connectionString; });
  27. });
  28. ////单个连接字符注入
  29. //services.Configure<AzureRedisFactoryOptions>(name, o => { o.Name = name; o.RedisConnectionString = connectionString; });
  30. return services;
  31. }
  32. }
  33. }