|
| 1 | +#if false |
| 2 | +using System; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Options; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.Configuration.Binder; |
| 7 | +using Microsoft.Extensions.Primitives; |
| 8 | + |
| 9 | +// ReSharper disable once CheckNamespace |
| 10 | +namespace Microsoft.Extensions.Configuration |
| 11 | +{ |
| 12 | + public static class ConfigureByConfigurationPathExtension |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Registers a injected configuration service which TOptions will bind against. |
| 16 | + /// </summary> |
| 17 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 18 | + /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |
| 19 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 20 | + public static IServiceCollection Configure<TOptions>(this IServiceCollection services) |
| 21 | + where TOptions : class |
| 22 | + { |
| 23 | + if (services == null) |
| 24 | + { |
| 25 | + throw new ArgumentNullException(nameof(services)); |
| 26 | + } |
| 27 | + |
| 28 | + return Configure<TOptions>(services, null); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Registers a injected configuration service which TOptions will bind against. |
| 33 | + /// </summary> |
| 34 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 35 | + /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |
| 36 | + /// <param name="sectionName">The name of the options instance.</param> |
| 37 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 38 | + public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string? sectionName) |
| 39 | + where TOptions : class |
| 40 | + { |
| 41 | + if (services == null) |
| 42 | + { |
| 43 | + throw new ArgumentNullException(nameof(services)); |
| 44 | + } |
| 45 | + |
| 46 | + services.AddOptions(); |
| 47 | + services.AddSingleton<IOptionsChangeTokenSource<TOptions>>( |
| 48 | + _ => new ConfigurationChangeTokenSource<TOptions>( |
| 49 | + Options.Options.DefaultName, |
| 50 | + sectionName == null ? _.GetRequiredService<IConfiguration>() : _.GetRequiredService<IConfiguration>().GetSection(sectionName) |
| 51 | + ) |
| 52 | + ); |
| 53 | + return services.AddSingleton<IConfigureOptions<TOptions>>( |
| 54 | + _ => new NamedConfigureFromConfigurationOptions<TOptions>( |
| 55 | + Options.Options.DefaultName, |
| 56 | + sectionName == null ? _.GetRequiredService<IConfiguration>() : _.GetRequiredService<IConfiguration>().GetSection(sectionName) |
| 57 | + ) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Registers a injected configuration service which TOptions will bind against. |
| 63 | + /// </summary> |
| 64 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 65 | + /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |
| 66 | + /// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param> |
| 67 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 68 | + public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<BinderOptions> configureBinder) |
| 69 | + where TOptions : class |
| 70 | + { |
| 71 | + if (services == null) |
| 72 | + { |
| 73 | + throw new ArgumentNullException(nameof(services)); |
| 74 | + } |
| 75 | + |
| 76 | + return Configure<TOptions>(services, Options.Options.DefaultName, configureBinder); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Registers a injected configuration service which TOptions will bind against. |
| 81 | + /// </summary> |
| 82 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 83 | + /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |
| 84 | + /// <param name="sectionName">The name of the options instance.</param> |
| 85 | + /// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param> |
| 86 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 87 | + public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string sectionName, Action<BinderOptions> configureBinder) |
| 88 | + where TOptions : class |
| 89 | + { |
| 90 | + if (services == null) |
| 91 | + { |
| 92 | + throw new ArgumentNullException(nameof(services)); |
| 93 | + } |
| 94 | + |
| 95 | + services.AddOptions(); |
| 96 | + services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(_ => new ConfigurationChangeTokenSource<TOptions>(Options.Options.DefaultName, _.GetRequiredService<IConfiguration>().GetSection(sectionName))); |
| 97 | + return services.AddSingleton<IConfigureOptions<TOptions>>(_ => new NamedConfigureFromConfigurationOptions<TOptions>(Options.Options.DefaultName, _.GetRequiredService<IConfiguration>().GetSection(sectionName), configureBinder)); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Registers a injected configuration service which TOptions will bind against. |
| 102 | + /// </summary> |
| 103 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 104 | + /// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param> |
| 105 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 106 | + public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder) |
| 107 | + where TOptions : class |
| 108 | + { |
| 109 | + if (builder == null) |
| 110 | + { |
| 111 | + throw new ArgumentNullException(nameof(builder)); |
| 112 | + } |
| 113 | + |
| 114 | + return Configure(builder, Options.Options.DefaultName); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Registers a injected configuration service which TOptions will bind against. |
| 119 | + /// </summary> |
| 120 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 121 | + /// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param> |
| 122 | + /// <param name="sectionName">The name of the options instance.</param> |
| 123 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 124 | + public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, string sectionName) |
| 125 | + where TOptions : class |
| 126 | + { |
| 127 | + if (builder == null) |
| 128 | + { |
| 129 | + throw new ArgumentNullException(nameof(builder)); |
| 130 | + } |
| 131 | + |
| 132 | + Configure<TOptions>(builder.Services, name); |
| 133 | + return builder; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Registers a injected configuration service which TOptions will bind against. |
| 138 | + /// </summary> |
| 139 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 140 | + /// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param> |
| 141 | + /// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param> |
| 142 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 143 | + public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, Action<BinderOptions> configureBinder) |
| 144 | + where TOptions : class |
| 145 | + { |
| 146 | + if (builder == null) |
| 147 | + { |
| 148 | + throw new ArgumentNullException(nameof(builder)); |
| 149 | + } |
| 150 | + |
| 151 | + return Configure(builder, Options.Options.DefaultName, configureBinder); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Registers a injected configuration service which TOptions will bind against. |
| 156 | + /// </summary> |
| 157 | + /// <typeparam name="TOptions">The type of options being configured.</typeparam> |
| 158 | + /// <param name="builder">The <see cref="OptionsBuilder<TOptions>"/> to configure.</param> |
| 159 | + /// <param name="sectionName">The name of the options instance.</param> |
| 160 | + /// <param name="configureBinder">Used to configure the <see cref="BinderOptions"/>.</param> |
| 161 | + /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> |
| 162 | + public static OptionsBuilder<TOptions> Configure<TOptions>(this OptionsBuilder<TOptions> builder, string sectionName, Action<BinderOptions> configureBinder) |
| 163 | + where TOptions : class |
| 164 | + { |
| 165 | + if (builder == null) |
| 166 | + { |
| 167 | + throw new ArgumentNullException(nameof(builder)); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + Configure<TOptions>(builder.Services, name, configureBinder); |
| 172 | + return builder; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +#endif |
0 commit comments