Repository navigation
Description
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
dotnet-policy-service commentedon Mar 30, 2026
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
svick commentedon Mar 30, 2026
This is closely related to #58930. Though I think this being a regression means it's not a duplicate.
tarekgh commentedon Mar 30, 2026
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.
dotnet-policy-service commentedon Mar 30, 2026
This issue has been marked
needs-author-actionand may be missing some important information.svick commentedon Mar 30, 2026
@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.)
tarekgh commentedon Mar 30, 2026
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.
tarekgh commentedon Mar 30, 2026
Just to mention, the other workaround is to define the options as:
Fix ConfigurationBinder failing to bind empty array to constructor pa…
Fix ConfigurationBinder failing to bind empty array to constructor pa…
Fix ConfigurationBinder failing to bind empty array to constructor pa…