Skip to content

🐞 The WebApplicationBuilder.Configuration add custom configuration provider was called more than once. #36452

@monksoul

Description

@monksoul

In my project, I created a custom configuration provider, which is mainly used to process .txt files.

After that, I added a custom configuration provider through the following code and found that the provider's Load method was called multiple times.

The following code is added through WebApplicationBuilder.Configuration.

/// <summary>
/// TXTConfigurationSource.Build() call multiple times
/// </summary>
[Fact]
public void TestWebApplicationBuilder()
{
    var builder = WebApplication.CreateBuilder();
    builder.Configuration.AddTXTConfiguration(options =>
    {
        options.Output = Output;
        options.Path = Path.Combine(AppContext.BaseDirectory, "file.txt");
    });

    using var app = builder.Build();
    var services = app.Services;

    var configuration = services.GetRequiredService<IConfiguration>();
    Assert.Equal("VALUE", configuration["TXT"]);
}

image


However, if I use the Host.CreateDefaultBuilder().ConfigureAppConfiguration() method to add a custom configuration provider, the Load method will only be called once, which is what I expect.

/// <summary>
/// TXTConfigurationSource.Build() call once
/// </summary>
[Fact]
public void TestHostBuilder()
{
    var builder = Host.CreateDefaultBuilder()
        .ConfigureAppConfiguration((context, builder) =>
        {
            builder.AddTXTConfiguration(options =>
            {
                options.Output = Output;
                options.Path = Path.Combine(AppContext.BaseDirectory, "file.txt");
            });
        });

    var app = builder.Build();
    var services = app.Services;

    var configuration = services.GetRequiredService<IConfiguration>();
    Assert.Equal("VALUE", configuration["TXT"]);
}

image


🟡 Test Code:https://github.com/MonkSoul/net6-customize-configuration-souce/tree/master/ConfigurationTests

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-minimal-hostingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions