Skip to content

ConfigurationBinder fails to bind empty array in Net 10.0 #126312

Closed

Description

@da1910

Description

We have a configuration type which defines an array field, in net8.0 we were able to have an entry in our appsettings.json file which was an empty array and we could safely handle it. It was unhelpful that it arrived as null, but you can pass a null to the constructor and handle it safely.

In net 10 the behaviour has "improved" - see #36510 - specifically #36510 (comment)

Now this is arguably better in some ways, but it's extremely surprising, since a typed config object now cannot safely handle deserialization of this kind of value. Rather than null which passes the type check and can then be caught with a null guard in the constructor, the Binder now throws:

Unhandled exception. System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.String[]'.
   at System.RuntimeType.CheckValue(Object& value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type, IConfiguration config, BinderOptions options, ParameterInfo[]& constructorParameters)
   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)

Reproduction Steps

Run in dotnetfiddle against .NET 8 to see an empty array, and against .NET 10 to see the exception.

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

RunTest("Empty array []",              @"{ ""ArrayField"": [] }");

static void RunTest(string label, string json)
{
    Console.WriteLine($"\n=== {label} ===");
    try
    {
        var config = new ConfigurationBuilder()
            .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
            .Build();

        var options = config.Get<MyOptions>();

        var values = options.ArrayField;
        Console.WriteLine($"  Bound: {values.Length} item(s): [{string.Join(", ", values)}]");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"  FAIL — {ex.GetType().Name}: {ex.Message}");
    }
}

public class MyOptions
{
	public MyOptions(string[] arrayField = null)
	{
		ArrayField = arrayField ?? Array.Empty<string>();
	}
	
	public string[] ArrayField { get; }
}

Expected behavior

Object is deserialized into an empty array with no trouble - specifically null is passed and caught

Actual behavior

ArgumentException: Object of type 'System.String' cannot be converted to type 'System.String[]'.

Regression?

Regression from .net9.0 and .net8.0 (which I was using before)

Known Workarounds

Removing the key entirely works, as does passing [null] or [""] but neither of these are the same object as []

Configuration

Net 10 - 10.0.4
Windows 11 x64

Other information

Most of the fixes in the issue are improvements but this seems like a clear regression, as we can no longer safely type string[] entries and expect that they will be deserialized from valid json inputs. At the least it ought to have a documentation warning to make clear that this is not a valid operation, or it needs fixing properly.

Activity

  1. added
    untriaged
    on Mar 30, 2026
  2. dotnet-policy-service commented on Mar 30, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-configuration
    See info in area-owners.md if you want to be subscribed.

  3. svick commented on Mar 30, 2026

    @svick
    Member

    This is closely related to #58930. Though I think this being a regression means it's not a duplicate.

  4. tarekgh commented on Mar 30, 2026

    @tarekgh
    Member

    This should be fixed by #121325 and duplicate of #121193. @da1910 please try to install the latest .NET 10 https://dotnet.microsoft.com/en-us/download/dotnet/10.0 and let's know if you still see the issue.

  5. added
    needs-author-action
    on Mar 30, 2026
  6. dotnet-policy-service commented on Mar 30, 2026

    @dotnet-policy-service
    Contributor

    This issue has been marked needs-author-action and may be missing some important information.

  7. svick commented on Mar 30, 2026

    @svick
    Member

    @tarekgh I can reproduce this on 10.0.5 and 11.0.0 Preview 2. So I don't think it was fixed. (One difference is that this is binding through a constructor parameter, not a property.)

  8. removed
    needs-author-action
    on Mar 30, 2026
  9. tarekgh commented on Mar 30, 2026

    @tarekgh
    Member

    I was referencing the wrong version of the configuration library. Now I can see the exception. Enabling the source generator can work round this issue, I think.

    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  10. tarekgh commented on Mar 30, 2026

    @tarekgh
    Member

    Just to mention, the other workaround is to define the options as:

        public class MyOptions
        {
            public MyOptions()
            {
                ArrayField = Array.Empty<string>();
            }
    
            public MyOptions(string[] arrayField)
            {
                ArrayField = arrayField ?? Array.Empty<string>();
            }
    
            public string[] ArrayField { get; }
        }
  11. self-assigned this
    on Mar 31, 2026
  12. removed
    untriaged
    on Mar 31, 2026
  13. added a commit that references this issue on Apr 2, 2026
    d860e5a
  14. added a commit that references this issue on Apr 9, 2026
    3c83542
  15. locked and limited conversation to collaborators on May 10, 2026
  16. added this to the 11.0.0 milestone on Jul 1, 2026
  17. added a commit that references this issue on Jul 15, 2026
    3494f28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions