-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PollyServiceCollectionExtensions.cs
295 lines (262 loc) · 14 KB
/
PollyServiceCollectionExtensions.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Polly.DependencyInjection;
using Polly.Registry;
using Polly.Telemetry;
using Polly.Utils;
namespace Polly;
/// <summary>
/// Provides extension methods for registering resilience pipelines using the <see cref="IServiceCollection"/>.
/// </summary>
public static class PollyServiceCollectionExtensions
{
/// <summary>
/// Adds a resilience pipeline that handles <typeparamref name="TResult"/> to service collection.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <typeparam name="TResult">The type of result that the resilience pipeline handles.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <param name="key">The key used to identify the resilience pipeline.</param>
/// <param name="configure">An action that configures the resilience pipeline.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the registered resilience pipeline.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.</exception>
/// <remarks>
/// You can retrieve the registered pipeline by resolving the <see cref="ResiliencePipelineProvider{TKey}"/> class from the dependency injection container.
/// <para>
/// This call enables telemetry for the registered resilience pipeline.
/// </para>
/// </remarks>
public static IServiceCollection AddResiliencePipeline<TKey, TResult>(
this IServiceCollection services,
TKey key,
Action<ResiliencePipelineBuilder<TResult>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
return services.AddResiliencePipeline<TKey, TResult>(key, (builder, _) => configure(builder));
}
/// <summary>
/// Adds a resilience pipeline that handles <typeparamref name="TResult"/> to service collection.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <typeparam name="TResult">The type of result that the resilience pipeline handles.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <param name="key">The key used to identify the resilience pipeline.</param>
/// <param name="configure">An action that configures the resilience pipeline.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the registered resilience pipeline.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.</exception>
/// <remarks>
/// You can retrieve the registered pipeline by resolving the <see cref="ResiliencePipelineProvider{TKey}"/> class from the dependency injection container.
/// <para>
/// This call enables telemetry for the registered resilience pipeline.
/// </para>
/// </remarks>
public static IServiceCollection AddResiliencePipeline<TKey, TResult>(
this IServiceCollection services,
TKey key,
Action<ResiliencePipelineBuilder<TResult>, AddResiliencePipelineContext<TKey>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
services.TryAddKeyedTransient(
key,
(serviceProvider, key) =>
{
var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProvider<TKey>>();
return pipelineProvider.GetPipeline<TResult>((TKey)key!);
});
return services.AddResiliencePipelines<TKey>((context) =>
{
context.AddResiliencePipeline(key, configure);
});
}
/// <summary>
/// Adds a resilience pipeline to service collection.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <param name="key">The key used to identify the resilience pipeline.</param>
/// <param name="configure">An action that configures the resilience pipeline.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the registered resilience pipeline.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.</exception>
/// <remarks>
/// You can retrieve the registered pipeline by resolving the <see cref="ResiliencePipelineProvider{TKey}"/> class from the dependency injection container.
/// <para>
/// This call enables telemetry for the registered resilience pipeline.
/// </para>
/// </remarks>
public static IServiceCollection AddResiliencePipeline<TKey>(
this IServiceCollection services,
TKey key,
Action<ResiliencePipelineBuilder> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
return services.AddResiliencePipeline(key, (builder, _) => configure(builder));
}
/// <summary>
/// Adds a resilience pipeline to service collection.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <param name="key">The key used to identify the resilience pipeline.</param>
/// <param name="configure">An action that configures the resilience pipeline.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the registered resilience pipeline.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.</exception>
/// <remarks>
/// You can retrieve the registered pipeline by resolving the <see cref="ResiliencePipelineProvider{TKey}"/> class from the dependency injection container.
/// <para>
/// This call enables telemetry for the registered resilience pipeline.
/// </para>
/// </remarks>
public static IServiceCollection AddResiliencePipeline<TKey>(
this IServiceCollection services,
TKey key,
Action<ResiliencePipelineBuilder, AddResiliencePipelineContext<TKey>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
services.TryAddKeyedTransient(
key,
(serviceProvider, key) =>
{
var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProvider<TKey>>();
return pipelineProvider.GetPipeline((TKey)key!);
});
return services.AddResiliencePipelines<TKey>((context) =>
{
context.AddResiliencePipeline(key, configure);
});
}
/// <summary>
/// Allows deferred addition of one or more resilience pipelines to the service collection.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipelines.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add resilience pipelines to.</param>
/// <param name="configure">An action that allows configuration of resilience pipelines.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the addition configuration added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This method can be useful if you want to add resilience pipelines as late as possible, e.g.
/// to allow other services / configuration to be available before providing the key.
/// <para>
/// You can retrieve the registered pipeline by resolving the <see cref="ResiliencePipelineProvider{TKey}"/> class from the dependency injection container.
/// </para>
/// <para>
/// This call enables telemetry for the registered resilience pipeline.
/// </para>
/// </remarks>
public static IServiceCollection AddResiliencePipelines<TKey>(
this IServiceCollection services,
Action<AddResiliencePipelinesContext<TKey>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
services
.AddOptions<ConfigureResiliencePipelineRegistryOptions<TKey>>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
configure(new AddResiliencePipelinesContext<TKey>(options, serviceProvider));
});
return services.AddResiliencePipelineRegistry<TKey>();
}
/// <summary>
/// Adds <see cref="ResiliencePipelineRegistry{TKey}"/> and <see cref="ResiliencePipelineProvider{TKey}"/> to the service collection that allows configuring
/// and retrieving resilience pipelines using the <typeparamref name="TKey"/> key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <param name="configure">The action that configures the <see cref="ResiliencePipelineRegistryOptions{TKey}"/> that are used by the registry.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with additional services added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This call enables telemetry for all resilience pipelines created using <see cref="ResiliencePipelineRegistry{TKey}"/>.
/// </remarks>
public static IServiceCollection AddResiliencePipelineRegistry<TKey>(
this IServiceCollection services,
Action<ResiliencePipelineRegistryOptions<TKey>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);
services.AddResiliencePipelineRegistry<TKey>();
services.Configure(configure);
return services;
}
/// <summary>
/// Adds <see cref="ResiliencePipelineRegistry{TKey}"/> and <see cref="ResiliencePipelineProvider{TKey}"/> to the service collection that allows configuring
/// and retrieving resilience pipelines using the <typeparamref name="TKey"/> key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience pipeline to.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with additional services added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This call enables telemetry for all resilience pipelines created using <see cref="ResiliencePipelineRegistry{TKey}"/>.
/// </remarks>
public static IServiceCollection AddResiliencePipelineRegistry<TKey>(this IServiceCollection services)
where TKey : notnull
{
Guard.NotNull(services);
// Check marker to ensure the APIs below are called only once for each TKey type
// this prevents polluting the service collection with unnecessary Configure calls
if (services.Contains(RegistryMarker<TKey>.ServiceDescriptor))
{
return services;
}
services.AddOptions();
services.Add(RegistryMarker<TKey>.ServiceDescriptor);
services.AddResiliencePipelineBuilder();
services.TryAddSingleton(serviceProvider =>
{
var options = serviceProvider.GetRequiredService<IOptions<ResiliencePipelineRegistryOptions<TKey>>>().Value;
var configureActions = serviceProvider.GetRequiredService<IOptions<ConfigureResiliencePipelineRegistryOptions<TKey>>>().Value.Actions;
var registry = new ResiliencePipelineRegistry<TKey>(options);
foreach (var entry in configureActions)
{
entry(registry);
}
return registry;
});
services.TryAddSingleton<ResiliencePipelineProvider<TKey>>(serviceProvider => serviceProvider.GetRequiredService<ResiliencePipelineRegistry<TKey>>());
// configure options
services
.AddOptions<ResiliencePipelineRegistryOptions<TKey>>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
options.BuilderFactory = () => serviceProvider.GetRequiredService<ResiliencePipelineBuilder>();
});
return services;
}
private static void AddResiliencePipelineBuilder(this IServiceCollection services)
{
services
.AddOptions<TelemetryOptions>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
options.LoggerFactory = serviceProvider.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
});
services.TryAddTransient(serviceProvider =>
{
var builder = new ResiliencePipelineBuilder();
builder.ConfigureTelemetry(serviceProvider.GetRequiredService<IOptions<TelemetryOptions>>().Value);
if (serviceProvider.GetService<TimeProvider>() is { } timeProvider)
{
builder.TimeProvider = timeProvider;
}
return builder;
});
}
private class RegistryMarker<TKey>
{
public static readonly ServiceDescriptor ServiceDescriptor = ServiceDescriptor.Singleton(new RegistryMarker<TKey>());
}
}