Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration binding ignores TypeConverter when binding against a IConfigurationSection #105195

Open
rchoffardet opened this issue Jul 21, 2024 · 2 comments

Comments

@rchoffardet
Copy link

Description

When binding configuration, it's normally possible to override the conversion by adding a TypeConverterAttribute attribute to the field, property, or class declaration. It does work when binding against a string value but not against a IConfigurationSection.

Reproduction Steps

https://dotnetfiddle.net/gxB9o3

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;


var configuration = new ConfigurationBuilder()
    .AddInMemoryCollection(new Dictionary<string, string>()
    {
        { "0", "Hello"},
        { "1", "world"},
    })
    .Build();

try
{
    var options = configuration.Get<Item>();
    Console.WriteLine("Binding success.");
}
catch (Exception e)
{
    Console.WriteLine("Binding failure.");
}

[TypeConverter(typeof(ItemConverter))]
class Item
{
    private string Zero;
    private string One;

    public Item(string zero, string one)
    {
        Zero = zero;
        One = one;
    }
}

class ItemConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
    {
        return sourceType == typeof(IConfigurationSection);
    }

    public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        var section = (IConfigurationSection)value;
        return new Item(section.GetValue<string>("0"), section.GetValue<string>("1"));
    }
}

Expected behavior

Item is correctly bound

Actual behavior

Failing this message: "Cannot create instance of type 'Item' because one or more parameters cannot be bound to. Constructor parameters must have corresponding properties. Fields are not supported. Missing properties are: 'zero,one'"

Regression?

Not that I know of

Known Workarounds

None that I know of

Configuration

  • Runtime: .NET 8.0.5
  • OS: Windows 11 Version 23H2
  • Architecture: x64
    It's probably not specific to that configuration, AFAIK, it's the same behavior in .NET 9 preview versions

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 21, 2024
Copy link
Contributor

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

@tarekgh tarekgh added this to the Future milestone Jul 21, 2024
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Jul 21, 2024
@rchoffardet
Copy link
Author

I'm willing to put effort into this, here is a draft PR for this: #105228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants