-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAddResiliencePipelineContext.cs
More file actions
72 lines (64 loc) · 3.15 KB
/
AddResiliencePipelineContext.cs
File metadata and controls
72 lines (64 loc) · 3.15 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
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
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Polly.Registry;
namespace Polly.DependencyInjection;
/// <summary>
/// Represents the context for adding a resilience pipeline with the specified key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience pipeline.</typeparam>
public sealed class AddResiliencePipelineContext<TKey>
where TKey : notnull
{
internal AddResiliencePipelineContext(ConfigureBuilderContext<TKey> registryContext, IServiceProvider serviceProvider)
{
RegistryContext = registryContext;
ServiceProvider = serviceProvider;
}
/// <summary>
/// Gets the pipeline key for the pipeline being created.
/// </summary>
public TKey PipelineKey => RegistryContext.PipelineKey;
/// <summary>
/// Gets the <see cref="IServiceProvider"/> that provides access to the dependency injection container.
/// </summary>
public IServiceProvider ServiceProvider { get; }
/// <summary>
/// Gets the context that is used by the registry.
/// </summary>
internal ConfigureBuilderContext<TKey> RegistryContext { get; }
/// <summary>
/// Enables dynamic reloading of the resilience pipeline whenever the <typeparamref name="TOptions"/> options are changed.
/// </summary>
/// <typeparam name="TOptions">The options type to listen to.</typeparam>
/// <param name="name">The named options, if any.</param>
/// <remarks>
/// You can decide based on the <paramref name="name"/> to listen for changes in global options or named options.
/// If <paramref name="name"/> is <see langword="null"/> then the global options are listened to.
/// <para>
/// You can listen for changes from multiple options by calling this method with different <typeparamref name="TOptions"/> types.
/// </para>
/// </remarks>
public void EnableReloads<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>(string? name = null)
=> RegistryContext.EnableReloads(ServiceProvider.GetRequiredService<IOptionsMonitor<TOptions>>(), name);
/// <summary>
/// Gets the options identified by <paramref name="name"/>.
/// </summary>
/// <typeparam name="TOptions">The options type.</typeparam>
/// <param name="name">The options name, if any.</param>
/// <returns>The options instance.</returns>
/// <remarks>
/// If <paramref name="name"/> is <see langword="null"/> then the global options are returned.
/// </remarks>
public TOptions GetOptions<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>(string? name = null)
{
var monitor = ServiceProvider.GetRequiredService<IOptionsMonitor<TOptions>>();
return monitor.Get(name);
}
/// <summary>
/// Registers a callback that is called when the pipeline instance being configured is disposed.
/// </summary>
/// <param name="callback">The callback delegate.</param>
public void OnPipelineDisposed(Action callback)
=> RegistryContext.OnPipelineDisposed(callback);
}