-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathConfigureByConfigurationPathExtension.cs
176 lines (159 loc) · 8.7 KB
/
ConfigureByConfigurationPathExtension.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
#if false
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Binder;
using Microsoft.Extensions.Primitives;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Configuration
{
public static class ConfigureByConfigurationPathExtension
{
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services)
where TOptions : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return Configure<TOptions>(services, null);
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="sectionName">The name of the options instance.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string? sectionName)
where TOptions : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddOptions();
services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(
_ => new ConfigurationChangeTokenSource<TOptions>(
Options.Options.DefaultName,
sectionName == null ? _.GetRequiredService<IConfiguration>() : _.GetRequiredService<IConfiguration>().GetSection(sectionName)
)
);
return services.AddSingleton<IConfigureOptions<TOptions>>(
_ => new NamedConfigureFromConfigurationOptions<TOptions>(
Options.Options.DefaultName,
sectionName == null ? _.GetRequiredService<IConfiguration>() : _.GetRequiredService<IConfiguration>().GetSection(sectionName)
)
);
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<BinderOptions> configureBinder)
where TOptions : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return Configure<TOptions>(services, Options.Options.DefaultName, configureBinder);
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="sectionName">The name of the options instance.</param>
/// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string sectionName, Action<BinderOptions> configureBinder)
where TOptions : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddOptions();
services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(_ => new ConfigurationChangeTokenSource<TOptions>(Options.Options.DefaultName, _.GetRequiredService<IConfiguration>().GetSection(sectionName)));
return services.AddSingleton<IConfigureOptions<TOptions>>(_ => new NamedConfigureFromConfigurationOptions<TOptions>(Options.Options.DefaultName, _.GetRequiredService<IConfiguration>().GetSection(sectionName), configureBinder));
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder)
where TOptions : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return Configure(builder, Options.Options.DefaultName);
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param>
/// <param name="sectionName">The name of the options instance.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, string sectionName)
where TOptions : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
Configure<TOptions>(builder.Services, name);
return builder;
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param>
/// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, Action<BinderOptions> configureBinder)
where TOptions : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return Configure(builder, Options.Options.DefaultName, configureBinder);
}
/// <summary>
/// Registers a injected configuration service which TOptions will bind against.
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param>
/// <param name="sectionName">The name of the options instance.</param>
/// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, string sectionName, Action<BinderOptions> configureBinder)
where TOptions : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
Configure<TOptions>(builder.Services, name, configureBinder);
return builder;
}
}
}
#endif