-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration binding ignores ErrorOnUnknownConfiguration and throws #98231
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionAccording to a breaking change in .NET 8, configuration binding should throw an To avoid this behavior, the documentation mentions that you can avoid this by setting This does not work, though, and an exception is thrown when the configuration value can't be converted to the type in the model. Reproduction Steps
using Microsoft.Extensions.Configuration;
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection([
// Empty string, can't be converted to integer.
KeyValuePair.Create<string, string?>("IntegerValue", "")
]);
var configuration = builder.Build();
// This throws an InvalidOperationException.
var options = configuration.Get<TestOptions>(o => o.ErrorOnUnknownConfiguration = false);
// Expect default value of 11
Console.WriteLine(options?.IntegerValue);
public class TestOptions {
public int IntegerValue { get; set; } = 11;
} Expected behaviorNo exception thrown, according to description in https://learn.microsoft.com/en-us/dotnet/core/compatibility/extensions/8.0/configurationbinder-exceptions#recommended-action. Actual behaviorException thrown when trying to bind configuration to options instance. This will also occur when using
Regression?I haven't tested, but I assume so, since this was the reason for the breaking change. Known WorkaroundsNo response Configuration.NET 8.0.0 Other informationRelated issue: #73915
|
To clarify, this is not a regression. The current behavior is same as pervious .NET versions behavior. It looks we missed the case that is using type converters. we need to handle the cases when calling runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs Line 312 in a79c62d
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs Line 923 in a79c62d
|
@tarekgh Hello, any update on this item? Do you have timeline for this fix? |
@galakt We currently have higher-priority tasks at hand. If you're interested in submitting a pull request for those, we welcome your contribution. |
We are also running into this issue and can potentially help fix it with some guidance, if no one else is working on this |
Can we also get this breaking change for .Net 8 documentation changed? It's currently the same behavior as in .Net 6, where type mismatch errors during configuration binding always throws an error by default. |
@krmayankk feel free to submit a PR if you are interested. My comment #98231 (comment) explain what need to be done I guess. |
Description
According to a breaking change in .NET 8, configuration binding should throw an
InvalidOperationException
if the value in the configuration can't be converted to the type of value in the model.To avoid this behavior, the documentation mentions that you can avoid this by setting
BinderOptions.ErrorOnUnknownConfiguration
tofalse
.This does not work, though, and an exception is thrown when the configuration value can't be converted to the type in the model.
Reproduction Steps
Expected behavior
No exception thrown, according to description in https://learn.microsoft.com/en-us/dotnet/core/compatibility/extensions/8.0/configurationbinder-exceptions#recommended-action.
Actual behavior
Exception thrown when trying to bind configuration to options instance. This will also occur when using
.Configure<TOptions>
and usingIOptions<TOptions>
via the DI framework.Regression?
I haven't tested, but I assume so, since this was the reason for the breaking change.
Known Workarounds
No response
Configuration
.NET 8.0.0
Windows 11
x64
Other information
Related issue: #73915
The text was updated successfully, but these errors were encountered: