-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAopPollyServiceCollectionExtensions.cs
More file actions
39 lines (34 loc) · 1.39 KB
/
AopPollyServiceCollectionExtensions.cs
File metadata and controls
39 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Polly.Registry;
namespace ZibStack.NET.Aop;
/// <summary>
/// DI registration for Polly-based aspect handlers.
/// </summary>
public static class AopPollyServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="PollyRetryHandler"/> and <see cref="PollyHttpRetryHandler"/> in DI.
/// Call after <c>AddAop()</c>.
///
/// <para>
/// If <see cref="ResiliencePipelineProvider{TKey}"/> is available in DI
/// (e.g. via <c>AddResiliencePipeline</c>), <see cref="PollyRetryHandler"/> uses it
/// for named pipeline resolution. Otherwise only inline pipelines are supported.
/// </para>
/// </summary>
public static IServiceCollection AddAopPolly(this IServiceCollection services)
{
if (services is null) throw new ArgumentNullException(nameof(services));
services.TryAddSingleton<PollyRetryHandler>(sp =>
{
var provider = sp.GetService<ResiliencePipelineProvider<string>>();
return provider is not null ? new PollyRetryHandler(provider) : new PollyRetryHandler();
});
services.TryAddSingleton<PollyHttpRetryHandler>();
services.TryAddSingleton<PollyCircuitBreakerHandler>();
services.TryAddSingleton<PollyRateLimiterHandler>();
return services;
}
}