diff --git a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs index ff8e9cdeef3..89a96652ea1 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs @@ -25,6 +25,23 @@ public static TBuilder AddStrategy(this TBuilder builder, ResilienceSt return builder.AddStrategy(_ => strategy, EmptyOptions.Instance); } + /// + /// Adds an already created strategy instance to the builder. + /// + /// The type of the result. + /// The builder instance. + /// The strategy instance. + /// The same builder instance. + /// Thrown when is null. + /// Thrown when this builder was already used to create a strategy. The builder cannot be modified after it has been used. + public static ResilienceStrategyBuilder AddStrategy(this ResilienceStrategyBuilder builder, ResilienceStrategy strategy) + { + Guard.NotNull(builder); + Guard.NotNull(strategy); + + return builder.AddStrategy(strategy.Strategy); + } + /// /// Adds a strategy to the builder. /// diff --git a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs index 07462ca655d..c0adcb7742d 100644 --- a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs +++ b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs @@ -51,4 +51,19 @@ public void Build_Ok() strategy.Should().NotBeNull(); strategy.Strategy.Should().BeOfType().Subject.Strategies.Should().HaveCount(2); } + + [Fact] + public void AddGenericStrategy_Ok() + { + // arrange + var testStrategy = new ResilienceStrategy(new TestResilienceStrategy()); + _builder.AddStrategy(testStrategy); + + // act + var strategy = _builder.Build(); + + // assert + strategy.Should().NotBeNull(); + strategy.Strategy.Should().Be(testStrategy.Strategy); + } }