You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
When settings model has property of type 'object', ConfigurationBinder throws InvalidOperationException ('Failed to convert 'VALUE' to type 'System.Object').
I think this is a bug, because property with type 'object' can accept value of any type.
If settings model has a property with array of another model that has property of type 'object', this array entry is ignored (because of 'catch {}' code, see below).
Steps to reproduce
Settings models:
public class MySettingsWithArray {
public MySettings[] Lst { get; set; }
}
public class MySettings {
public object Id { get; set; }
}
IConfigurationRoot Configuration; // lets assume this is our app config
var testOpts = new MySettings();
Configuration.GetSection("TEST").Bind(testOpts); // throws an exception, but
var test2Opts = new MySettingsWithArray();
Configuration.GetSection("TEST2").Bind(test2Opts);
// test2Opts.Lst[0] is null , exception is not thrown
Exception may be fixed with simple type check in ConfigurationBinder.ConvertValue(Type type, string value).
Both array & collection de-serialization methods (ConfigurationBinder.BindArray, ConfigurationBinder.BindCollection) have a code that ignores any exceptions during array/collection members deserialization:
for (int i = 0; i < children.Length; i++)
{
try
{
var item = BindInstance(
type: elementType,
instance: null,
config: children[i]);
if (item != null)
{
newArray.SetValue(item, arrayLength + i);
}
}
catch
{
}
}
This is definitely weird behaviour, when in some cases ConfigurationBinder.Bind throws an exceptions, and when array/collection is used they are ignored.
The text was updated successfully, but these errors were encountered:
When settings model has property of type 'object', ConfigurationBinder throws InvalidOperationException ('Failed to convert 'VALUE' to type 'System.Object').
I think this is a bug, because property with type 'object' can accept value of any type.
If settings model has a property with array of another model that has property of type 'object', this array entry is ignored (because of 'catch {}' code, see below).
Steps to reproduce
Settings models:
appsettings.json:
Bind code that works incorrectly:
Exception may be fixed with simple type check in ConfigurationBinder.ConvertValue(Type type, string value).
Both array & collection de-serialization methods (ConfigurationBinder.BindArray, ConfigurationBinder.BindCollection) have a code that ignores any exceptions during array/collection members deserialization:
This is definitely weird behaviour, when in some cases ConfigurationBinder.Bind throws an exceptions, and when array/collection is used they are ignored.
The text was updated successfully, but these errors were encountered: