Skip to content

MinimalAPI configuration reload do not update the the options value #34056

Closed
@WeihanLi

Description

@WeihanLi

Describe the bug

When I use the MinimalAPI, I met an issue that the value of the options could not get updated when I try to reload the configuration via IConfigurationRoot.Reload(), I'm not sure if this is by design.

Sample code is as below:

var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<SettingsOption>(builder.Configuration);
var app = builder.Build();

var logger = app.Services.GetRequiredService<ILoggerFactory>()
    .CreateLogger("Test");
app.Map(new PathString("/redeploy"), appBuilder => appBuilder.Run(context =>
{
    var config = context.RequestServices.GetRequiredService<IConfiguration>();
    logger.LogInformation($"Configuration in redeploy hash code: {config.GetHashCode()}");
    if (config is IConfigurationRoot configuration)
    {
        var currentSlot = configuration["Setting1"];
        configuration["Setting1"] = "Slot2" != currentSlot ? "Slot2" : "Slot1";
        configuration.Reload();
        return context.Response.WriteAsync("Success");
    }
    logger.LogWarning($"configuration is not configuration root, configurationType: {config.GetType().FullName}");
    return Task.CompletedTask;
}));
app.MapFallback(context =>
{
    var option = context.RequestServices.GetRequiredService<IOptionsMonitor<SettingsOption>>();
    return context.Response.WriteAsJsonAsync(new{ option.CurrentValue.Setting1, Time= DateTime.Now });
});
app.Run();

When I access the redeploy endpoint, the value of the option should be updated I think.

More

After some research, I found that the builder.Configuration and IConfiguration from DI are not the same instance, so when I call reload, the option's value did not update, maybe this is by design? I'm not sure so I created this issue.

To Reproduce

There's a simple sample for reproducing the issue, https://github.com/WeihanLi/AspNetCorePlayground/tree/master/MinimalAPIConfigurationIssue

  1. Run dotnet run
  2. Access the endpoint http://localhost:5000
  3. Access the endpoint http://localhost:5000/redeploy
  4. Reply step 2, access http:localhost:5000 again

Expected behavior:

The setting1 value from the response should be Slot2

Actucal behavior:

The setting1 value from the response keep as null

Further technical details

  • ASP.NET Core version: .NET 6 Preview 5
  • Include the output of dotnet --info
    image

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-hosting

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions