forked from App-vNext/Polly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ConfigureResiliencePipelineRegistry<TKey> method to IServiceCol…
…lection to allow deferred configuration of ResiliencePipelineRegistry<TKey> (App-vNext#1860) * Added ConfigureResiliencePipelineRegistry<TKey> method to IServiceCollection to allow deferred configuration of ResiliencePipelineRegistry<TKey> * Reworked per feedback from initial implementation * Removed previous implementation that was restored when I synced the changes last. * Added unit tests to ensure pipelines added correctly via new AddResiliencePipelines method * Fixed several small formatting issues and added guards to stop user-provided delegates being null
- Loading branch information
1 parent
2e4fde1
commit 37107b0
Showing
6 changed files
with
249 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Polly.Extensions/DependencyInjection/AddResiliencePipelinesContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Polly.Utils; | ||
|
||
namespace Polly.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Represents the context for configuring resilience pipelines with the specified key. | ||
/// </summary> | ||
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam> | ||
public sealed class AddResiliencePipelinesContext<TKey> | ||
where TKey : notnull | ||
{ | ||
private readonly ConfigureResiliencePipelineRegistryOptions<TKey> _options; | ||
|
||
internal AddResiliencePipelinesContext( | ||
ConfigureResiliencePipelineRegistryOptions<TKey> options, | ||
IServiceProvider serviceProvider) | ||
{ | ||
_options = options; | ||
ServiceProvider = serviceProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IServiceProvider"/> that provides access to the dependency injection container. | ||
/// </summary> | ||
public IServiceProvider ServiceProvider { get; } | ||
|
||
/// <summary> | ||
/// Adds a resilience pipeline to the registry. | ||
/// </summary> | ||
/// <param name="key">The key used to identify the resilience pipeline.</param> | ||
/// <param name="configure">An action that configures the resilience pipeline.</param> | ||
/// <remarks> | ||
/// You can retrieve the registered pipeline by resolving the <see cref="Registry.ResiliencePipelineProvider{TKey}"/> class from the dependency injection container. | ||
/// <para> | ||
/// This call enables the telemetry for the registered resilience pipeline. | ||
/// </para> | ||
/// </remarks> | ||
public void AddResiliencePipeline( | ||
TKey key, | ||
Action<ResiliencePipelineBuilder, AddResiliencePipelineContext<TKey>> configure) | ||
{ | ||
Guard.NotNull(configure); | ||
|
||
_options.Actions.Add((registry) => | ||
{ | ||
// the last added builder with the same key wins, this allows overriding the builders | ||
registry.TryAddBuilder(key, (builder, context) => | ||
{ | ||
configure(builder, new AddResiliencePipelineContext<TKey>(context, ServiceProvider)); | ||
}); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a resilience pipeline to the registry. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of result that the resilience pipeline handles.</typeparam> | ||
/// <param name="key">The key used to identify the resilience pipeline.</param> | ||
/// <param name="configure">An action that configures the resilience pipeline.</param> | ||
/// <remarks> | ||
/// You can retrieve the registered pipeline by resolving the <see cref="Registry.ResiliencePipelineProvider{TKey}"/> class from the dependency injection container. | ||
/// <para> | ||
/// This call enables the telemetry for the registered resilience pipeline. | ||
/// </para> | ||
/// </remarks> | ||
public void AddResiliencePipeline<TResult>( | ||
TKey key, | ||
Action<ResiliencePipelineBuilder<TResult>, AddResiliencePipelineContext<TKey>> configure) | ||
{ | ||
Guard.NotNull(configure); | ||
|
||
_options.Actions.Add((registry) => | ||
{ | ||
// the last added builder with the same key wins, this allows overriding the builders | ||
registry.TryAddBuilder<TResult>(key, (builder, context) => | ||
{ | ||
configure(builder, new AddResiliencePipelineContext<TKey>(context, ServiceProvider)); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
#nullable enable | ||
static Polly.PollyServiceCollectionExtensions.AddResiliencePipelines<TKey>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action<Polly.DependencyInjection.AddResiliencePipelinesContext<TKey>!>! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! | ||
Polly.DependencyInjection.AddResiliencePipelinesContext<TKey> | ||
Polly.DependencyInjection.AddResiliencePipelinesContext<TKey>.AddResiliencePipeline(TKey key, System.Action<Polly.ResiliencePipelineBuilder!, Polly.DependencyInjection.AddResiliencePipelineContext<TKey>!>! configure) -> void | ||
Polly.DependencyInjection.AddResiliencePipelinesContext<TKey>.AddResiliencePipeline<TResult>(TKey key, System.Action<Polly.ResiliencePipelineBuilder<TResult>!, Polly.DependencyInjection.AddResiliencePipelineContext<TKey>!>! configure) -> void | ||
Polly.DependencyInjection.AddResiliencePipelinesContext<TKey>.ServiceProvider.get -> System.IServiceProvider! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters