Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options Configuration with dynamic (runtime) values

I have a MySettings class which I want to configure as Options to make it available via the dependency injection.

Currently I do (in an service builder extension method):

services.Configure<MySettings>(configuration.GetSection(MySettings.CustomSectionName));

My problem is that one part of the settings comes from the appsettings and other values are only known at runtime (startup).

So I try to find out how to configure the settings with adding the runtime provided values. I tried to add the values to the configuration

configuration["SectionName:ValueX"] = "my runtime value";

That did not work and ValueX is always null (when the options are injected in the controller).

Any suggestions for me?

like image 935
monty Avatar asked Sep 11 '25 21:09

monty


1 Answers

You can use IPostConfigureOptions< TOptions > interface to achieve what you want

services.PostConfigure<CustomOptions>(customOptions =>
{
    customOptions.Option1 = "post_configured_option1_value";
});

Reference: https://learn.microsoft.com/en-us/dotnet/core/extensions/options#options-post-configuration

like image 148
Maksim Avatar answered Sep 13 '25 14:09

Maksim