backtrack5r3
backtrack5r3
CC#
Created by backtrack5r3 on 7/19/2023 in #help
❔ 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
4 replies
CC#
Created by backtrack5r3 on 5/2/2023 in #help
❔ Get Console App args inside Docker container with Docker-compose
Hi, I have trouble with a .NET6 Console App and Docker with the use of args. If I launch the Console App without Docker in VS22 everything is okay, my args are retrieved, but when I use with Docker container inside a Docker-compose nothing happen... I tried to edit the Dockerfile to hardcode the value of the args, use the "command" part of docker-compose but nothing is working. Someone have a solution ? I would like to not use env variable to handle args. Thank you, Tom.
45 replies