Description
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
- Run
dotnet run
- Access the endpoint http://localhost:5000
- Access the endpoint http://localhost:5000/redeploy
- 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