Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 22, 2025

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 IConfigurationBuilder to load configuration values from an appsettings.json file 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.json file 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:

var builder = MauiApp.CreateBuilder();
builder.Configuration.AddLocalAppSettings();
var app = builder.Build();

var configuration = app.Services.GetRequiredService<IConfiguration>();
Console.WriteLine(configuration["AppName"]); // "My MAUI App"
Console.WriteLine(configuration["Settings:EnableLogging"]); // "true"
Console.WriteLine(configuration["Features:0"]); // "Feature1"

Generated code

The source generator creates an extension method with hardcoded values from your JSON:

public static IConfigurationBuilder AddLocalAppSettings(this IConfigurationBuilder configurationBuilder)
{
    var configurationData = new Dictionary<string, string>
    {
        { "AppName", "My MAUI App" },
        { "Settings:EnableLogging", "true" },
        { "Settings:MaxConnections", "100" },
        { "ConnectionString", "Server=localhost;Database=MyDB" },
        { "Features:0", "Feature1" },
        { "Features:1", "Feature2" },
    };
    return configurationBuilder.AddInMemoryCollection(configurationData);
}

Key features

  • Build-time generation: No runtime JSON parsing overhead
  • Flattened keys: Supports nested objects and arrays using standard configuration key format (Settings:EnableLogging, Features:0)
  • Type preservation: Numbers, booleans, and strings are properly converted
  • Incremental compilation: Only regenerates when appsettings.json changes
  • Zero dependencies: Uses only built-in .NET configuration APIs

Implementation details

  • Source generator: Microsoft.Maui.Core.ConfigurationSourceGen using IIncrementalGenerator
  • Integration: Included as analyzer in Microsoft.Maui.Core package
  • Distribution: Automatically available to all MAUI applications
  • Testing: Comprehensive unit tests covering JSON parsing, code generation, and edge cases

The source generator integrates seamlessly with the existing MAUI build process and requires no additional setup - just add an appsettings.json file to your project and use builder.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.

Copilot AI and others added 3 commits August 22, 2025 20:03
…tion

Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
…working

Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Copilot AI changed the title [WIP] Can you create a source generator that runs during a build and generates an extension method for IConfigurationBuilder that generates an InMemoryCollection for a appsettings.json file at the root of the project. So, if i have this faile in my app, the... Add AppSettings source generator for IConfigurationBuilder extension Aug 22, 2025
Copilot AI requested a review from mattleibow August 22, 2025 20:14
@jfversluis
Copy link
Member

/azp run MAUI-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jfversluis
Copy link
Member

Cool idea, lets revisit later

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants