C
C#17mo ago
backtrack5r3

❔ Exception while using .BindConfiguration instead of .Configure

Hello ! I need some help to understand why I get an
System.InvalidOperationException
System.InvalidOperationException
while I am using Option Pattern (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0) with dependency injection. The exception :
System.InvalidOperationException: No constructor for type 'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1[CustomOptions]' can be instantiated using services from the service container and default values.
System.InvalidOperationException: No constructor for type 'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1[CustomOptions]' can be instantiated using services from the service container and default values.
My tests are using a custom helper which iterate through a IServiceCollection to retrieve service from default IoC by using IServiceProvider.GetService( ServiceDescriptor.ServiceType). At the beginning I was registering my options this way (which cause the exception spoken above) :
services.AddOptions<CustomOptions>()
.BindConfiguration(CustomOptions.SectionName);
services.AddOptions<CustomOptions>()
.BindConfiguration(CustomOptions.SectionName);
I changed the registration for this way (which work perfect) :
services.AddOptions<CustomOptions>()
.Configure<IConfiguration>((options, configuration) =>
{
const string sectionName = CustomOptions.SectionName;
var section = configuration.GetSection(sectionName);

if (!section.Exists())
throw new OptionsValidationException(sectionName, typeof(CustomOptions),
new[] { $"Missing section {sectionName}." });

section.Bind(options);
})
services.AddOptions<CustomOptions>()
.Configure<IConfiguration>((options, configuration) =>
{
const string sectionName = CustomOptions.SectionName;
var section = configuration.GetSection(sectionName);

if (!section.Exists())
throw new OptionsValidationException(sectionName, typeof(CustomOptions),
new[] { $"Missing section {sectionName}." });

section.Bind(options);
})
The difference on the code from .BindConfiguration and .Configure : - .BindConfiguration use this instruction:
optionsBuilder.Services.AddSingleton<IOptionsChangeTokenSource<TOptions>, ConfigurationChangeTokenSource<TOptions>>();
optionsBuilder.Services.AddSingleton<IOptionsChangeTokenSource<TOptions>, ConfigurationChangeTokenSource<TOptions>>();
-.Configure use this instructio :
Services.AddTransient<IConfigureOptions<TOptions>>(sp =>
new ConfigureNamedOptions<TOptions, TDep>(Name, sp.GetRequiredService<TDep>(), configureOptions));
Services.AddTransient<IConfigureOptions<TOptions>>(sp =>
new ConfigureNamedOptions<TOptions, TDep>(Name, sp.GetRequiredService<TDep>(), configureOptions));
Can someone explain the difference and why the first one throw an exception please ? Thank you
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
3 Replies
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
backtrack5r3
backtrack5r3OP17mo ago
Hi, someon can up ? thanks !
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server