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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With