Skip to content

Loading external JSON files into IConfiguration #30451

Answered by stephenquan
TofuBug asked this question in Q&A
Discussion options

You must be logged in to vote

In your MauiProgram.cs, let's try registering the ConfigurationManager itself for DI, i.e.

builder.Services.AddSingleton<Microsoft.Extensions.Configuration.IConfigurationManager>(builder.Configuration);

Then, we can access the ConfigurationManager later, in, say, App.xaml.cs, and populate with late configurations:

using Microsoft.Extensions.Configuration;

public partial class App : Application
{
    public App(IConfigurationManager cm)
    {
        InitializeComponent();
        // populate Name with something ...
        cm.AddJsonFile($"{FileSystem.AppDataDirectory}/{Name}.json", true, true);
    }
    protected override Window CreateWindow(IActivationState? activationState)
    {

Replies: 2 comments 9 replies

Comment options

You must be logged in to vote
8 replies
@rokmeglic71
Comment options

@TofuBug
Comment options

@rokmeglic71
Comment options

@TofuBug
Comment options

@rokmeglic71
Comment options

Comment options

You must be logged in to vote
1 reply
@TofuBug
Comment options

Answer selected by TofuBug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants