Skip to content

ChainedConfigurationSource doesn't support reloading manually #58683

Closed

Description

@davidfowl

Description

This was found as a part of investigating dotnet/aspnetcore#36181. It seems like manually calling IConfigurationRoot.Reload on a chained configuration source doesn't work.

Configuration

var config = new ConfigurationBuilder()
        .Add(new RandomConfigurationSource())
        .Build();

var config2 = new ConfigurationBuilder()
            .AddConfiguration(config)
            .Build();

Console.WriteLine(config2["Random"]);

config2.Reload();

Console.WriteLine(config2["Random"]);

public class RandomConfigurationSource : IConfigurationSource
{
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new RandomConfigurationProvider();
    }
}

public class RandomConfigurationProvider : ConfigurationProvider
{
    public override void Load()
    {
        Data["Random"] = Guid.NewGuid().ToString();
    }
}

This should print 2 random guids, instead it prints the same guid. If you used a single configuration root (not chained), it would work.

Regression?

Nope

Activity

  1. ghost added
    untriaged
    on Sep 4, 2021
  2. removed
    untriaged
    on Sep 4, 2021
  3. added this to the Future milestone on Jan 25, 2022
  4. OwnageIsMagic commented on Feb 18, 2023

    @OwnageIsMagic
    Contributor

    I'm actually (ab)using this behavior to reorder configuration providers, without rebuilding the whole tree.

    var builder = WebApplication.CreateBuilder(Args);
    builder.Configuration.InsertConsul( new Uri("..."),
     ^2, // insert before before env and args
     "config1", "config2", "config3"
    );
    // ...
    
    static IConfigurationBuilder InsertConsul(this IConfigurationBuilder builder, Uri consulUri,
        Index insertPosition, params string[] cfgNodes)
    {
        IConfigurationBuilder? oldBuilder = null;
        if (!insertPosition.Equals(^0)) // not to the end
        {
            oldBuilder = builder;
            builder = new ConfigurationBuilder();
        }
    
        foreach (var cfgNode in cfgNodes)
        {
            builder.AddConsul(cfgNode, options =>
            {
                options.ConsulConfigurationOptions = cco => cco.Address = consulUri;
            });
        }
    
        if (oldBuilder != null)
        {
            // WebApplication's ConfigurationManager eagerly rebuilds configuration on Sources modification
            // Create ChainedSource wrapper to add them as one item; as side effect it also disables reload
            // Otherwise (inserting directly into ConfigurationManager.Sources) it will
            // rebuild configuration tree on each Insert (makes 1+2+3+N blocking HTTP calls, instead of N)
            var source = new ChainedConfigurationSource { Configuration = builder.Build(), ShouldDisposeConfiguration = true };
            builder = oldBuilder;
            var sources = builder.Sources;
            sources.Insert(insertPosition.GetOffset(sources.Count), source);
        }
    
        return builder;
    }

    It would be nice to have some API to temporally pause eager configuration evaluation. Or some other way to reorder providers.
    Can we just reorder providers and rebuild lazily (on first access)?

  5. rosebyte commented on Mar 3, 2026

    @rosebyte
    Member

    Triage: we should fix it.

  6. added a commit that references this issue on Mar 3, 2026
    a3de21d
  7. added
    in-pr
    on Mar 3, 2026
  8. added a commit that references this issue on Mar 13, 2026
    0b874d8
  9. added a commit that references this issue on Mar 13, 2026
    9b92210
  10. locked and limited conversation to collaborators on Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions