Add AppSettings source generator for IConfigurationBuilder extension #31298
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
This PR introduces a new source generator that automatically creates an extension method for
IConfigurationBuilderto load configuration values from anappsettings.jsonfile at build time.What does this solve?
Developers often need to include configuration data from JSON files in their MAUI applications. Previously, this required manually reading and parsing JSON files at runtime. This source generator automates the process by generating compile-time code that embeds the configuration values directly.
How it works
When you have an
appsettings.jsonfile in your project root:{ "AppName": "My MAUI App", "Settings": { "EnableLogging": true, "MaxConnections": 100 }, "ConnectionString": "Server=localhost;Database=MyDB", "Features": ["Feature1", "Feature2"] }The source generator automatically creates an extension method that you can use like this:
Generated code
The source generator creates an extension method with hardcoded values from your JSON:
Key features
Settings:EnableLogging,Features:0)appsettings.jsonchangesImplementation details
Microsoft.Maui.Core.ConfigurationSourceGenusingIIncrementalGeneratorMicrosoft.Maui.CorepackageThe source generator integrates seamlessly with the existing MAUI build process and requires no additional setup - just add an
appsettings.jsonfile to your project and usebuilder.Configuration.AddLocalAppSettings().✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.