MultipleAzureServiceBusFactoryExtensions.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 MultipleAzureServiceBusFactoryExtensions
  10. {
  11. /// <summary>
  12. /// Function注入
  13. /// </summary>
  14. /// <param name="services"></param>
  15. /// <param name="connectInfos"></param>
  16. /// <returns></returns>
  17. /// <exception cref="ArgumentNullException"></exception>
  18. /// <exception cref="AccessViolationException"></exception>
  19. public static IServiceCollection AddMultipleAzureServiceBus(this IServiceCollection services, List<(string name, string connectionString)> connectInfos)
  20. {
  21. if (services == null) throw new ArgumentNullException(nameof(services));
  22. if (connectInfos == null) throw new AccessViolationException(nameof(connectInfos));
  23. services.TryAddSingleton<AzureServiceBusFactory>();
  24. //多个注入
  25. connectInfos.ForEach(connect =>
  26. {
  27. services.Configure<AzureServiceBusFactoryOptions>(connect.name, o => { o.Name = connect.name; o.ServiceBusConnectionString = connect.connectionString; });
  28. });
  29. //services.Configure<AzureServiceBusFactoryOptions>(name, o => { o.Name = name; o.ServiceBusConnectionString = connectionString; });//单一注入
  30. return services;
  31. }
  32. }
  33. }