Skip to content

Configuration binding ignores ErrorOnUnknownConfiguration and throws #98231

Description

@avendel

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 to false.

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

dotnet new console
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Binder
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 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 using IOptions<TOptions> via the DI framework.

Unhandled exception. System.InvalidOperationException: Failed to convert configuration value at 'IntegerValue' to type 'System.Int32'.
 ---> System.ArgumentException:  is not a valid value for Int32. (Parameter 'value')
 ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   --- End of inner exception stack trace ---
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.TryConvertValue(Type type, String value, String path, Object& result, Exception& error)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, BindingPoint bindingPoint, IConfiguration config, BinderOptions options, Boolean isParentCollection)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperties(Object instance, IConfiguration configuration, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, BindingPoint bindingPoint, IConfiguration config, BinderOptions options, Boolean isParentCollection)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Get(IConfiguration configuration, Type type, Action`1 configureOptions)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration, Action`1 configureOptions)

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Feb 9, 2024
  2. tarekgh commented on Feb 9, 2024

    @tarekgh
    Member

    Regression?
    I haven't tested, but I assume so, since this was the reason for the breaking change.

    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 TryConvertValue to check ErrorOnUnknownConfiguration before throwing.


  3. added this to the Future milestone on Feb 9, 2024
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 9, 2024
  5. galakt commented on Apr 16, 2024

    @galakt
    Contributor

    @tarekgh Hello, any update on this item? Do you have timeline for this fix?

  6. tarekgh commented on Apr 16, 2024

    @tarekgh
    Member

    @galakt We currently have higher-priority tasks at hand. If you're interested in submitting a pull request for those, we welcome your contribution.

  7. krmayankk commented on Jun 23, 2024

    @krmayankk

    We are also running into this issue and can potentially help fix it with some guidance, if no one else is working on this

  8. vivinjoy commented on Jun 24, 2024

    @vivinjoy

    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.

  9. tarekgh commented on Jun 24, 2024

    @tarekgh
    Member

    @krmayankk feel free to submit a PR if you are interested. My comment #98231 (comment) explain what need to be done I guess.

  10. rosebyte commented on Mar 5, 2026

    @rosebyte
    Member

    Triage: we may investigate and potentially fix in future.

  11. self-assigned this
    on Aug 5, 2026
  12. added a commit that references this issue on Aug 6, 2026
    751b1d4
  13. added
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 6, 2026
  14. modified the milestones: Future, 12.0.0 on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions