diff --git a/src/Polly.Core.Tests/Strategy/OutcomeTests.cs b/src/Polly.Core.Tests/Strategy/OutcomeTests.cs index b529c9eb124..9532c2a8474 100644 --- a/src/Polly.Core.Tests/Strategy/OutcomeTests.cs +++ b/src/Polly.Core.Tests/Strategy/OutcomeTests.cs @@ -64,4 +64,20 @@ public void ToString_NullResult_ShouldBeEmpty() var outcome = new Outcome((object)null!); outcome.ToString().Should().BeEmpty(); } + + [Fact] + public void EnsureSuccess_Result() + { + var outcome = new Outcome("dummy"); + + outcome.Invoking(o => o.EnsureSuccess()).Should().NotThrow(); + } + + [Fact] + public void EnsureSuccess_Exception() + { + var outcome = new Outcome(new InvalidOperationException()); + + outcome.Invoking(o => o.EnsureSuccess()).Should().Throw(); + } } diff --git a/src/Polly.Core/Registry/ConfigureBuilderContext.cs b/src/Polly.Core/Registry/ConfigureBuilderContext.cs index 48208707384..0f505cebdc3 100644 --- a/src/Polly.Core/Registry/ConfigureBuilderContext.cs +++ b/src/Polly.Core/Registry/ConfigureBuilderContext.cs @@ -7,13 +7,7 @@ namespace Polly.Registry; public class ConfigureBuilderContext where TKey : notnull { - /// - /// Initializes a new instance of the class. - /// - /// The strategy key. - /// The builder name. - /// The strategy key as string. - public ConfigureBuilderContext(TKey strategyKey, string builderName, string strategyKeyString) + internal ConfigureBuilderContext(TKey strategyKey, string builderName, string strategyKeyString) { StrategyKey = strategyKey; BuilderName = builderName; diff --git a/src/Polly.Core/Strategy/Outcome.cs b/src/Polly.Core/Strategy/Outcome.cs index ed8f05186fa..b7002d17bc3 100644 --- a/src/Polly.Core/Strategy/Outcome.cs +++ b/src/Polly.Core/Strategy/Outcome.cs @@ -58,6 +58,14 @@ public Outcome(TResult result) /// public bool IsVoidResult => Result is VoidResult; + /// + /// Throws an exception if the operation produced an exception. + /// + /// + /// If the operation produced a result, this method does nothing. The thrown exception maintains its original stack trace. + /// + public void EnsureSuccess() => ExceptionDispatchInfo?.Throw(); + /// /// Tries to get the result, if available. /// diff --git a/src/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs b/src/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs index 5a9a6610a06..73870450ee4 100644 --- a/src/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs +++ b/src/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs @@ -95,6 +95,7 @@ public void AddResilienceStrategy_EnsureContextFilled(bool generic) { _services.AddResilienceStrategy(Key, (builder, context) => { + context.RegistryContext.Should().NotBeNull(); context.StrategyKey.Should().Be(Key); builder.Should().NotBeNull(); context.ServiceProvider.Should().NotBeNull(); diff --git a/src/Polly.Extensions/DependencyInjection/AddResilienceStrategyContext.cs b/src/Polly.Extensions/DependencyInjection/AddResilienceStrategyContext.cs index 87cae8ae332..114fcd9284b 100644 --- a/src/Polly.Extensions/DependencyInjection/AddResilienceStrategyContext.cs +++ b/src/Polly.Extensions/DependencyInjection/AddResilienceStrategyContext.cs @@ -6,14 +6,28 @@ namespace Polly.Extensions.DependencyInjection; /// Represents the context for adding a resilience strategy with the specified key. /// /// The type of the key used to identify the resilience strategy. -public sealed class AddResilienceStrategyContext : ConfigureBuilderContext +public sealed class AddResilienceStrategyContext where TKey : notnull { - internal AddResilienceStrategyContext(ConfigureBuilderContext context, IServiceProvider serviceProvider) - : base(context.StrategyKey, context.BuilderName, context.StrategyKeyString) => ServiceProvider = serviceProvider; + internal AddResilienceStrategyContext(ConfigureBuilderContext registryContext, IServiceProvider serviceProvider) + { + RegistryContext = registryContext; + ServiceProvider = serviceProvider; + StrategyKey = registryContext.StrategyKey; + } + + /// + /// Gets the strategy key for the strategy being created. + /// + public TKey StrategyKey { get; } /// /// Gets the that provides access to the dependency injection container. /// public IServiceProvider ServiceProvider { get; } + + /// + /// Gets the context that is used by the registry. + /// + public ConfigureBuilderContext RegistryContext { get; } }