Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WeihanLi opened this issue Jul 3, 2021 · 5 comments
Closed

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

WeihanLi opened this issue Jul 3, 2021 · 5 comments
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-hosting
Milestone

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented Jul 3, 2021

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
@halter73
Copy link
Member

halter73 commented Jul 7, 2021

This might be fixed by #33082

@WeihanLi
Copy link
Contributor Author

WeihanLi commented Jul 8, 2021

Thanks @halter73, maybe I should close this issue now or wait for the fix?

@halter73
Copy link
Member

halter73 commented Jul 8, 2021

Let's keep it open for now @WeihanLi. This provides a good test case, so thanks! I'll close it once I confirmed it's fixed.

@halter73 halter73 modified the milestones: 6.0-preview7, 6.0-rc1 Jul 12, 2021
@halter73
Copy link
Member

Thanks again for the report @WeihanLi. It turns out this was fixed by #33081 in preview6 which is now available.

image

I feel it's kind of cheating because we simply register WebApplicationBuilder.Configuration as an IConfiguration service after the inner HostBuilder's IConfiguration.

// Register Configuration as IConfiguration so updates can be observed even after the WebApplication is built.
Services.AddSingleton<IConfiguration>(Configuration);

This works when the IConfiguration is resolved from DI by middleware as is done in this sample, but there are some places where the inner HostBuilder is still using it's own _hostConfiguration, so it would be best to also use a ChainedConfigurationSource as suggested by #33082. I'll continue tracking that with this issue.

@halter73 halter73 modified the milestones: 6.0-rc1, 6.0-preview6 Jul 15, 2021
@WeihanLi
Copy link
Contributor Author

it would be best to also use a ChainedConfigurationSource as suggested by #33082

Agree with this, thanks @halter73

@ghost ghost locked as resolved and limited conversation to collaborators Aug 15, 2021
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-hosting
Projects
None yet
Development

No branches or pull requests

5 participants