|
| 1 | +--- |
| 2 | +title: Compile-time configuration source generation |
| 3 | +description: Learn how to use the configuration source generator to intercept specific call sites and bypass reflection-based configuration binding. |
| 4 | +author: IEvangelist |
| 5 | +ms.author: dapine |
| 6 | +ms.date: 10/09/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# Configuration source generator |
| 10 | + |
| 11 | +Starting with .NET 8, a configuration binding source generator was introduced that intercepts specific call sites and generates their functionality. This feature provides a [Native ahead-of-time (AOT)](../deploying/native-aot/index.md) and [trim-friendly](../deploying/trimming/trim-self-contained.md) way to use the [configuration binder](configuration.md#binding), without the use of the reflection-based implementation. Reflection requires dynamic code generation, which isn't supported in AOT scenarios. |
| 12 | + |
| 13 | +This feature is possible with the advent of [C# interceptors](/dotnet/csharp/whats-new/csharp-12#interceptors) that were introduced in C# 12. Interceptors allow the compiler to generate source code that intercepts specific calls and substitutes them with generated code. |
| 14 | + |
| 15 | +## Enable the configuration source generator |
| 16 | + |
| 17 | +To enable the configuration source generator, add the following property to your project file: |
| 18 | + |
| 19 | +```xml |
| 20 | +<PropertyGroup> |
| 21 | + <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator> |
| 22 | +</PropertyGroup> |
| 23 | +``` |
| 24 | + |
| 25 | +When the configuration source generator is enabled, the compiler generates a source file that contains the configuration binding code. The generated source intercepts binding APIs from the following classes: |
| 26 | + |
| 27 | +- <xref:Microsoft.Extensions.Configuration.ConfigurationBinder?displayProperty=fullName> |
| 28 | +- <xref:Microsoft.Extensions.DependencyInjection.OptionsBuilderConfigurationExtensions?displayProperty=fullName> |
| 29 | +- <xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions?displayProperty=nameWithType> |
| 30 | + |
| 31 | +In other words, all APIs that eventually call into these various binding methods are intercepted and replaced with generated code. |
| 32 | + |
| 33 | +## Example usage |
| 34 | + |
| 35 | +Consider a .NET console application configured to publish as a native AOT app. The following code demonstrates how to use the configuration source generator to bind configuration settings: |
| 36 | + |
| 37 | +:::code language="xml" source="snippets/configuration/console-binder-gen/console-binder-gen.csproj" highlight="9,11"::: |
| 38 | + |
| 39 | +The preceding project file enables the configuration source generator by setting the `EnableConfigurationBindingGenerator` property to `true`. |
| 40 | + |
| 41 | +Next, consider the _Program.cs_ file: |
| 42 | + |
| 43 | +:::code source="snippets/configuration/console-binder-gen/Program.cs" highlight="12-14"::: |
| 44 | + |
| 45 | +The preceding code: |
| 46 | + |
| 47 | +- Instantiates a configuration builder instance. |
| 48 | +- Calls <xref:Microsoft.Extensions.Configuration.MemoryConfigurationBuilderExtensions.AddInMemoryCollection%2A> and defines three configuration source values. |
| 49 | +- Calls <xref:Microsoft.Extensions.Configuration.IConfigurationBuilder.Build> to build the configuration. |
| 50 | +- Uses the <xref:Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue%2A?displayProperty=nameWithType> extension method to get the value for each configuration key. |
| 51 | + |
| 52 | +When the application is built, the configuration source generator intercepts the call to `GetValue<T>` and generates the binding code. |
| 53 | + |
| 54 | +> [!IMPORTANT] |
| 55 | +> When the `PublishAot` property is set to `true` (or any other AOT warnings are enabled) and the `EnabledConfigurationBindingGenerator` property is set to `false`, warning `IL2026` is raised. This warning indicates that members are attributed with [RequiresUnreferencedCode](xref:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute) may break when trimming. For more information, see [IL2026](/dotnet/core/deploying/trimming/trim-warnings/il2026). |
| 56 | +
|
| 57 | +### Explore the source generated code |
| 58 | + |
| 59 | +The following code is generated by the configuration source generator for the preceding example: |
| 60 | + |
| 61 | +```csharp |
| 62 | +// <auto-generated/> |
| 63 | +
|
| 64 | +#nullable enable annotations |
| 65 | +#nullable disable warnings |
| 66 | + |
| 67 | +// Suppress warnings about [Obsolete] member usage in generated code. |
| 68 | +#pragma warning disable CS0612, CS0618 |
| 69 | + |
| 70 | +namespace System.Runtime.CompilerServices |
| 71 | +{ |
| 72 | + using System; |
| 73 | + using System.CodeDom.Compiler; |
| 74 | + |
| 75 | + [GeneratedCode( |
| 76 | + "Microsoft.Extensions.Configuration.Binder.SourceGeneration", |
| 77 | + "8.0.10.31311")] |
| 78 | + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] |
| 79 | + file sealed class InterceptsLocationAttribute : Attribute |
| 80 | + { |
| 81 | + public InterceptsLocationAttribute(string filePath, int line, int column) |
| 82 | + { |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration |
| 88 | +{ |
| 89 | + using Microsoft.Extensions.Configuration; |
| 90 | + using System; |
| 91 | + using System.CodeDom.Compiler; |
| 92 | + using System.Globalization; |
| 93 | + using System.Runtime.CompilerServices; |
| 94 | + |
| 95 | + [GeneratedCode( |
| 96 | + "Microsoft.Extensions.Configuration.Binder.SourceGeneration", |
| 97 | + "8.0.10.31311")] |
| 98 | + file static class BindingExtensions |
| 99 | + { |
| 100 | + #region IConfiguration extensions. |
| 101 | + /// <summary> |
| 102 | + /// Extracts the value with the specified key and converts it to the specified type. |
| 103 | + /// </summary> |
| 104 | + [InterceptsLocation(@"C:\source\configuration\console-binder-gen\Program.cs", 12, 26)] |
| 105 | + [InterceptsLocation(@"C:\source\configuration\console-binder-gen\Program.cs", 13, 29)] |
| 106 | + [InterceptsLocation(@"C:\source\configuration\console-binder-gen\Program.cs", 14, 28)] |
| 107 | + public static T? GetValue<T>(this IConfiguration configuration, string key) => |
| 108 | + (T?)(BindingExtensions.GetValueCore(configuration, typeof(T), key) ?? default(T)); |
| 109 | + #endregion IConfiguration extensions. |
| 110 | + |
| 111 | + #region Core binding extensions. |
| 112 | + public static object? GetValueCore( |
| 113 | + this IConfiguration configuration, Type type, string key) |
| 114 | + { |
| 115 | + if (configuration is null) |
| 116 | + { |
| 117 | + throw new ArgumentNullException(nameof(configuration)); |
| 118 | + } |
| 119 | + |
| 120 | + IConfigurationSection section = configuration.GetSection(key); |
| 121 | + |
| 122 | + if (section.Value is not string value) |
| 123 | + { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + if (type == typeof(int)) |
| 128 | + { |
| 129 | + return ParseInt(value, () => section.Path); |
| 130 | + } |
| 131 | + else if (type == typeof(bool)) |
| 132 | + { |
| 133 | + return ParseBool(value, () => section.Path); |
| 134 | + } |
| 135 | + else if (type == typeof(global::System.Uri)) |
| 136 | + { |
| 137 | + return ParseSystemUri(value, () => section.Path); |
| 138 | + } |
| 139 | + |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + public static int ParseInt( |
| 144 | + string value, Func<string?> getPath) |
| 145 | + { |
| 146 | + try |
| 147 | + { |
| 148 | + return int.Parse( |
| 149 | + value, |
| 150 | + NumberStyles.Integer, |
| 151 | + CultureInfo.InvariantCulture); |
| 152 | + } |
| 153 | + catch (Exception exception) |
| 154 | + { |
| 155 | + throw new InvalidOperationException( |
| 156 | + $"Failed to convert configuration value at " + |
| 157 | + "'{getPath()}' to type '{typeof(int)}'.", |
| 158 | + exception); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public static bool ParseBool( |
| 163 | + string value, Func<string?> getPath) |
| 164 | + { |
| 165 | + try |
| 166 | + { |
| 167 | + return bool.Parse(value); |
| 168 | + } |
| 169 | + catch (Exception exception) |
| 170 | + { |
| 171 | + throw new InvalidOperationException( |
| 172 | + $"Failed to convert configuration value at " + |
| 173 | + "'{getPath()}' to type '{typeof(bool)}'.", |
| 174 | + exception); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static global::System.Uri ParseSystemUri( |
| 179 | + string value, Func<string?> getPath) |
| 180 | + { |
| 181 | + try |
| 182 | + { |
| 183 | + return new Uri( |
| 184 | + value, |
| 185 | + UriKind.RelativeOrAbsolute); |
| 186 | + } |
| 187 | + catch (Exception exception) |
| 188 | + { |
| 189 | + throw new InvalidOperationException( |
| 190 | + $"Failed to convert configuration value at " + |
| 191 | + "'{getPath()}' to type '{typeof(global::System.Uri)}'.", |
| 192 | + exception); |
| 193 | + } |
| 194 | + } |
| 195 | + #endregion Core binding extensions. |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +> [!NOTE] |
| 201 | +> This generated code is subject to change based on the version of the configuration source generator. |
| 202 | +
|
| 203 | +The generated code contains the `BindingExtensions` class, which contains the `GetValueCore` method that performs the actual binding. The `GetValue<T>` extension method calls the `GetValueCore` method and casts the result to the specified type. |
| 204 | + |
| 205 | +To see the generated code, set the `<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>` in the project file. This ensures that the files are visible to the developer for inspection. |
| 206 | + |
| 207 | +## See also |
| 208 | + |
| 209 | +- [Configuration in .NET](configuration.md) |
| 210 | +- [Roslyn: Interceptors feature](https://github.com/dotnet/roslyn/blob/main/docs/features/interceptors.md) |
| 211 | +- [Options pattern in .NET](options.md) |
0 commit comments