Skip to content

Implement proper parsing of primitives in config binder generator #83533

@layomia

Description

@layomia

from @eerhardt in #82179 (comment) (fyi @stephentoub)

A couple things here:

Parsing:

  1. In order to be consistent with the reflection based behavior, we need to catch the int.Parse exception thrown here, and wrap it in a InvalidOperationException and add the configuration path of the invalid data. This allows devs to easily know where the invalid data exists from the exception message.
  2. This should be int.Parse(valueString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);

See the following for how I did it when manually writing the bind code for Console Logging:

internal static bool ParseInt(IConfiguration configuration, string key, out int value)
{
if (configuration[key] is string valueString)
{
try
{
value = int.Parse(valueString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
return true;
}
catch (Exception e)
{
ThrowInvalidConfigurationException(configuration, key, typeof(int), e);
}
}
value = default;
return false;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions