Reputation: 431
I'm trying to create a custom ConfigureOptions
class implementing IConfigureNamedOptions
in .NET 4.8.
Here is the implementation
public class ConfigureAppSettings : IConfigureNamedOptions<AppOptions>
{
private readonly IConfiguration configuration;
public ConfigureAppSettings()
{
configuration = new ConfigurationBuilder().BuildAdapterConfiguration("appsettings.json");
}
public void Configure(AppOptions options)
{
Configure(nameof(AppOptions), options);
}
public void Configure(string name, AppOptions options)
{
IOptions<AppOptions> configurationOptions = Options.Create(configuration.GetSection(nameof(AppOptions)).Get<AppOptions>());
options = configurationOptions.Value;
}
}
When configuring the dependency injection container the services.ConfigureOptions<ConfigureAppSettings>();
is called.
But then, when I want to use IOptionsSnapshot<AppOptions>
, Value
is null
.
While debugging options
is set inside the Configure
method but when exists it, options
reference is not preserved and is null
.
How can I get the value set in Configure
method?
Side note: I need this implementation because on Configure
method I also need to call an HTTP service to the some options
properties.
Upvotes: 1
Views: 946