diff --git a/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs b/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs index 9fd93250483..e8a93beadf4 100644 --- a/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs +++ b/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs @@ -85,7 +85,7 @@ public static IServiceCollection AddResilienceStrategy( }); }); - return AddResilienceStrategy(services); + return AddResilienceStrategyRegistry(services); } /// @@ -156,24 +156,47 @@ public static IServiceCollection AddResilienceStrategy( }); }); - return AddResilienceStrategy(services); + return AddResilienceStrategyRegistry(services); } /// - /// Adds the infrastructure that allows configuring and retrieving resilience strategies using the key. + /// Adds and that allows configuring + /// and retrieving resilience strategies using the key. /// /// The type of the key used to identify the resilience strategy. /// The to add the resilience strategy to. + /// The action that configures the that are used by the registry. /// The updated with additional services added. /// Thrown when is . /// - /// You can retrieve the strategy registry by resolving the - /// or class from the dependency injection container. - /// /// This call enables telemetry for all resilience strategies created using . - /// /// - public static IServiceCollection AddResilienceStrategy(this IServiceCollection services) + public static IServiceCollection AddResilienceStrategyRegistry( + this IServiceCollection services, + Action> configure) + where TKey : notnull + { + Guard.NotNull(services); + Guard.NotNull(configure); + + services.AddResilienceStrategyRegistry(); + services.Configure(configure); + + return services; + } + + /// + /// Adds and that allows configuring + /// and retrieving resilience strategies using the key. + /// + /// The type of the key used to identify the resilience strategy. + /// The to add the resilience strategy to. + /// The updated with additional services added. + /// Thrown when is . + /// + /// This call enables telemetry for all resilience strategies created using . + /// + public static IServiceCollection AddResilienceStrategyRegistry(this IServiceCollection services) where TKey : notnull { Guard.NotNull(services); @@ -188,7 +211,6 @@ public static IServiceCollection AddResilienceStrategy(this IServiceCollec services.AddOptions(); services.Add(RegistryMarker.ServiceDescriptor); services.AddResilienceStrategyBuilder(); - services.AddResilienceStrategy(); services.TryAddSingleton(serviceProvider => { @@ -204,10 +226,7 @@ public static IServiceCollection AddResilienceStrategy(this IServiceCollec return registry; }); - services.TryAddSingleton>(serviceProvider => - { - return serviceProvider.GetRequiredService>(); - }); + services.TryAddSingleton>(serviceProvider => serviceProvider.GetRequiredService>()); // configure options services diff --git a/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs b/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs index 5b51244ebf5..454bbeb0b1c 100644 --- a/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs +++ b/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs @@ -255,15 +255,27 @@ public void AddResilienceStrategy_Multiple_Ok() } [Fact] - public void AddResilienceStrategyInfra_Ok() + public void AddResilienceStrategyRegistry_Ok() { - var provider = new ServiceCollection().AddResilienceStrategy().BuildServiceProvider(); + var provider = new ServiceCollection().AddResilienceStrategyRegistry().BuildServiceProvider(); provider.GetRequiredService>().Should().NotBeNull(); provider.GetRequiredService>().Should().NotBeNull(); provider.GetRequiredService().DiagnosticSource.Should().NotBeNull(); } + [Fact] + public void AddResilienceStrategyRegistry_ConfigureCallback_Ok() + { + Func formatter = s => s; + + var provider = new ServiceCollection().AddResilienceStrategyRegistry(options => options.InstanceNameFormatter = formatter).BuildServiceProvider(); + + provider.GetRequiredService>().Should().NotBeNull(); + provider.GetRequiredService>().Should().NotBeNull(); + provider.GetRequiredService>>().Value.InstanceNameFormatter.Should().Be(formatter); + } + private void AddResilienceStrategy(string key, Action? onBuilding = null) { _services.AddResilienceStrategy(key, builder =>