-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
What are you wanting to achieve?
In .NET 8, support for keyed services has been added. I think this can replace ResiliencePipelineProvider in many scenarios.
What code or approach do you have so far?
I did this little test:
public class MyApi2
{
private readonly ResiliencePipeline _pipeline;
public MyApi2([FromKeyedServices("my-pipeline2")] ResiliencePipeline pipeline)
{
_pipeline = pipeline;
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
await _pipeline.ExecuteAsync(
static async token =>
{
// Add your code here
},
cancellationToken);
}
}
public static class MyApi2Extensions
{
public static IServiceCollection AddMyApi2(this IServiceCollection services)
{
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 4
})
.Build();
services.AddKeyedSingleton<ResiliencePipeline>("my-pipeline2", pipeline);
services.AddSingleton<MyApi2>();
return services;
}
}This seems to work. The main benefit of this is there is no longer a need to mock ResiliencePipelineProvider in unit tests.
I have been a bit suspicious of keyed services, but for Polly it seems to be a perfect match 😊. But I’m a bit surprised there is no documentation about this, not any discussions. Are there any drawbacks with this approach? Feels like I’m missing something.
Additional context
No response
Reactions are currently unavailable