-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Developers can safely trim apps which need Configuration Binder #44493
Comments
Tagging subscribers to this area: @maryamariyan Issue meta data
|
@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? |
@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. |
How would this be consumed by non C# languages? |
Marking as needs-work because a lot of this is contingent on a non-released compiler feature; but that doesn't block experimenting with it as-adjusted in previews. |
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 |
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...) |
@layomia It's totally valid to use The razor generator does suppression via @captainsafia has been working on a different generator which uses the 'don't pass it' strategy that you can probably re-use almost wholesale. |
You can find a sample of the strategy used for the Minimal APIs generator at this PR: dotnet/sdk#29982 |
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 |
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 The fact that the generated code is always a fixed type ( Project 1: [assembly:InternalsVisible2(Project2)]
var config = new ConfigurationBuilder().Build();
var settings = new SomeOptions();
section.Bind(settings); Project 2: (has a var config = new ConfigurationBuilder().Build();
var settings = new MoreOptions();
section.Bind(settings); // 👈 error 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) |
Thanks @andrewlock - I've opened #86363 to address this. |
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. |
Closing this issue given the feature is largely implemented and is getting integrated up-stack. Remaining work is tracked in #79527 and this query. |
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 providesBind
andGet
methods that maps configuration values (IConfiguration
instances) to strongly-typed objects.Bind
takes an instance, whileGet
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:
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
, andGet
calls that we can retrieve type info from.Given the sample user code above, here's the code that would be generated:
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.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.
Technical details
What's next
#79527
Related docs
Initial design
The text was updated successfully, but these errors were encountered: