Repository navigation
Description
Edited by @layomia.
Background
Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read data (as key-value pairs) using a variety of sources such as settings files (e.g. appsettings.json), environment variables, Azure Key Vault etc.
At the core of this mechanism is ConfigurationBinder, an extension class in platform extensions that provides Bind and Get methods that maps configuration values (IConfiguration instances) to strongly-typed objects. Bind takes an instance, while Get creates one on behalf of the caller. The current approach currently uses reflection which causes issues for trimming and Native AOT.
Here's example of code that users write to invoke the binder:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
// ASP.NET Core apps configure and launch a host, responsible for app startup and lifetime management.
// Templates create a WebApplicationBuilder which contains the host and provides default configuration
// for the app that may be specified by users through the mechanisms shown above.
// `args` contains configuration values specified through command line
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Represents a section of application configuration values (key-value pairs).
// Implements `IConfiguration`.
IConfigurationSection section = builder.Configuration.GetSection("MyOptions");
// !! Configure call - to be replaced with source-gen'd implementation
// `builder.Services`: a collection of user or framework-provided services for the application to compose
// `Configure<MyOptions>(section)`: adds the configuration section to the service collection for use in their operations
// `MyOptions`: configuration shape/type that maps to command-line specified config values
builder.Services.Configure<MyOptions>(section);
// !! Get call - to be replaced with source-gen'd implementation
// Get a new instance of your options using values from the config section
MyOptions options0 = section.Get<MyOptions>();
// Bind config options to an existing instance
MyOptions options1 = new MyOptions();
section.Bind(myOptions1);
// Build the application.
WebApplication app = builder.Build();
// Specify a http GET operation
app.MapGet("/", () => "Hello World!");
// Run the app
app.Run();
// Configuration classes mapping to user-provided values
public class MyOptions
{
public int A { get; set; }
public string S { get; set; }
public byte[] Data { get; set; }
public Dictionary<string, string> Values { get; set; }
public List<MyClass> Values2 { get; set; }
}
public class MyClass
{
public int SomethingElse { get; set; }
}Configuration binding source generator in .NET 8
In .NET 8, as a replacement to reflection, we wish to ship a source generator that generates static code to bind config values to target user types. We are proposing an implicit mechanism where we probe for Configure, Bind, and Get calls that we can retrieve type info from.
Given the sample user code above, here's the code that would be generated:
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensison.DependencyInjection;
public static class GeneratedConfigurationBinder
{
public static IServiceCollection Configure<T>(this IServiceCollection services, IConfiguration configuration)
{
if (typeof(T) == typeof(MyOptions))
{
// Redirect to generated Bind method.
return services.Configure<MyOptions>(obj => Bind(configuration, obj));
}
// Repeat if-check for all input types.
throw new NotSupportedException("The source generator did not detect this type as input.");
}
public static T? Get<T>(this global::Microsoft.Extensions.Configuration.IConfiguration configuration)
{
if (typeof(T) == typeof(MyOptions))
{
MyOptions obj = new();
Bind(configuration, obj);
return (T)(object)obj;
}
// Repeat if-check for all input types.
throw new NotSupportedException("The source generator did not detect this type as input.");
}
internal static void Bind(this IConfiguration configuration, MyOptions obj)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
// Preliminary code to validate input configuration.
obj.A = configuration["A"] is String stringValue0 ? int.Parse(stringValue4) : default;
obj.S = configuration["S"] is String stringValue1 ? bool.Parse(stringValue5) : default;
obj.Values ??= new();
Bind(configuration.GetSection("Values"), obj.Values);
obj.Values2 ?? = new();
Bind(configuration.GetSection("Values2"), obj.Values2);
}
private static void Bind(this IConfiguration configuration, Dictionary<string, string> obj)
{
if (obj is null)
{
throw new global::System.ArgumentNullException(nameof(obj));
}
string key;
string element;
foreach (Microsoft.Extensions.Configuration.IConfigurationSection section in configuration.GetChildren())
{
key = section["Key"] is string stringValue2 ? stringVal2 : default;
element = section["Value"] is string stringValue3 ? stringValue3 : default;
obj[key] = element;
}
}
private static void Bind(this IConfiguration configuration, List<MyClass> obj)
{
throw new NotSupportedException("This type is not supported.");
}
}Generator kick-off gesture
We want the user experience to be easy and minimal. Starting off, the generator will simply probe for a compiler visible property called UseConfigurationBinderSourceGenerator to determine whether to run. This allows folks that don't want the generator or for which it doesn't work for (yet) to skip using it.
<ItemGroup>
<CompilerVisibleProperty Include="UseConfigurationBinderSourceGenerator" />
</ItemGroup>
We'll add this as an SDK property later.
Generation strategy:
The operation is to convert IConfiguration instances into target config types. We'll do this by generate bespoke parsing/mapping logic for each target type. This is similar to a "fast-path" deserialization feature that the System.Text.Json source generator could implement (#55043).
PR: #82179
Integration with user apps
The code is generated into the global namespace and the methods are picked over the reflection by the compiler.
- As a next step we want to use the call site replace feature being developed by Roslyn to replace user calls with generated calls directly.
Technical details
- This approach satisfies the direct-call scenarios in user apps. As a next step we need to design a solution for indirect usage, e.g. calls the ASP.NET makes on behalf of users. A design isn't fully laid out yet but early ideas are to have some sort of global cache from implicit registration of generated code.
Activity
eerhardt commentedon Nov 10, 2020
@danroth27 @davidfowl - I don't see ConfigurationBinder being used on the WASM side of a Blazor application. Would a source generator only be useful for a server side ASP.NET app? Or is it useful for Blazor WASM as well?
I see this work item as being of the same importance as #44432, so I marked it as Priority 2 as well. Any thoughts there?
danroth27 commentedon Nov 10, 2020
@eerhardt Users could decide to use ConfigurationBinder themselves, but as far as I know it isn't commonly used. So I agree that this is really more about ASP.NET Core than Blazor.
maryamariyan commentedon Nov 11, 2020
Linking this issue to #43930 and #36130
7sharp9 commentedon Nov 17, 2020
How would this be consumed by non C# languages?
[-]Source Generator for Configuration Binder so apps can be trimmed, and native AOT compiled[/-][+]Developer can safely trim apps which need Configuration Binder[/+]33 remaining items
chsienki commentedon Feb 21, 2023
Want to call this out as probably not the right approach. It's pretty difficult to 'disable' a generator based on a property it reads in, and you end up with a lot of complicated logic (we do this in Razor for other reasons and it's not pleasant). From a performance POV you're also still doing a (small) amount of work even when it doesn't run.
Instead, you should adjust the logic in
ResolveTargetingPackAssets(or add another target that runs after it) to add/remove the generator being passed as part of theAnalyzerscollection altogether, based on the property provided by the user. This keeps the generator simpler, and ensure the least amount of work is done when it's not in use.layomia commentedon Feb 21, 2023
Thanks @chsienki I'll look into this approach. FYI @ericerhardt
Does the source generator cookbook need to be updated then?
Can you pls point me to where this is done? (or cc @captainsafia if you know...)
chsienki commentedon Feb 21, 2023
@layomia It's totally valid to use
CompilerVisiblePropertyas described in the cookbook, I'm just saying for this scenario it's not very useful. It's better to just not pass the generator in, instead of trying to supress it in code.The razor generator does suppression via
CompilerVisiblePropertybut it can't switch to the other mechanism due to some hot reload weirdness that we're fixing. When thats done, we'll use the 'don't pass it' strategy too.@captainsafia has been working on a different generator which uses the 'don't pass it' strategy that you can probably re-use almost wholesale.
captainsafia commentedon Feb 21, 2023
You can find a sample of the strategy used for the Minimal APIs generator at this PR: dotnet/sdk#29982
ericstj commentedon Feb 22, 2023
I think one of the challenges faced with this one is which targets should get the removal: https://github.com/dotnet/sdk/blob/45a14239002eb0a6f8ea6096d756d882a0cb8208/src/WebSdk/Web/Sdk/Sdk.props#L71-L73. This generator is not part of an SDK. It's associated with a library that's in the ASP.NET shared framework and also part of a NuGet package (to be used outside of ASP.NET). Do you recommend we put them in the .NET SDK and the
Microsoft.Extensions.Configuration.Bindernuget package?andrewlock commentedon May 16, 2023
One potential issue I can foresee with the design of the generated code (because I've run into a similar issue with my own generators) is around
internalclasses andInternalsVisibleTo.The fact that the generated code is always a fixed type (
GeneratedConfigurationBinder) in the global namespace, means that something like this will no longer compile:Project 1:
Project 2: (has a
<ProjectReference>to Project1)Gives
You might consider it an edge case, but it was one of the first issues I ran into when building my first source generator.
Note: I've just seen that preview 4 bits are going onto NuGet, so apologies if this is no longer relevant! :D (Edit: looks like the preview 3 package is broken for me)
layomia commentedon May 17, 2023
Thanks @andrewlock - I've opened #86363 to address this.
davidfowl commentedon May 17, 2023
This was the only edge case I found and it does feel like an edge case. That said, we can figure out a cleaner way to handle it.
layomia commentedon Jul 21, 2023
Closing this issue given the feature is largely implemented and is getting integrated up-stack. Remaining work is tracked in #79527 and this query.