MultipleAzureCosmosFactoryExtensions.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.DependencyInjection.Extensions;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using TEAMModelOS.SDK.DI;
  8. namespace TEAMModelOS.SDK.DI.Multiple
  9. {
  10. public static class MultipleAzureCosmosFactoryExtensions
  11. {
  12. /// <summary>
  13. /// cosmosDB注入
  14. /// </summary>
  15. /// <param name="services"></param>
  16. /// <param name="connectionInfo"></param>
  17. /// <returns></returns>
  18. /// <exception cref="ArgumentNullException"></exception>
  19. public static IServiceCollection AddMultipleAzureCosmos(this IServiceCollection services, List<(string name, string connectionString)> connectionInfo)
  20. {
  21. if (services == null) throw new ArgumentNullException(nameof(services));
  22. if (connectionInfo == null) throw new ArgumentNullException(nameof(connectionInfo));
  23. connectionInfo.ForEach(connection =>
  24. {
  25. services.Configure<AzureCosmosFactoryOptions>(connection.name, o => { o.Name = connection.name; o.CosmosConnectionString = connection.connectionString; });
  26. });
  27. services.AddSingleton<IOptionsMonitor<AzureCosmosFactoryOptions>, OptionsMonitor<AzureCosmosFactoryOptions>>();
  28. services.TryAddSingleton<AzureCosmosFactory>();
  29. //多个数据库注入
  30. return services;
  31. }
  32. }
  33. }