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

Microsoft.Extensions.Configuration.ConfigurationBinder should support byte[] #37384

Closed
AceHack opened this issue Jun 4, 2020 · 3 comments
Closed
Labels
area-Extensions-Configuration question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@AceHack
Copy link

AceHack commented Jun 4, 2020

Description

byte[] is not supported with the current strongly typed Microsoft.Extensions.Configuration. Please support byte[].

Other information

It seems pretty simple and just supporting base64 format would be fine. This would work with all configuration providers such as in-memory, json, XML, ini, environment, command line, etc...

Thanks.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Configuration untriaged New issue has not been triaged by the area owner labels Jun 4, 2020
@ghost
Copy link

ghost commented Jun 4, 2020

Tagging subscribers to this area: @safern
Notify danmosemsft if you want to be subscribed.

@ericstj ericstj added enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Jul 7, 2020
@ericstj ericstj added this to the Future milestone Jul 7, 2020
@ericstj ericstj added customer assistance and removed enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors labels Jul 7, 2020
@ericstj
Copy link
Member

ericstj commented Jul 7, 2020

So today the ConfigurationBinder relies on TypeConverter infrastructure to convert to target types. There is no built in typecoverter that does what you want, but you can register one.

See the following example:

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

namespace binder
{
    class Program
    {
        static void Main()
        {
            TypeDescriptor.AddAttributes(typeof(byte[]), new TypeConverterAttribute(typeof(Base64ByteArrayConverter)));

            var configuration = new ConfigurationBuilder()
                .AddInMemoryCollection(new Dictionary<string, string>()
                {
                    ["Bytes"] = "SGVsbG8="
                })
                .Build();

            var data = configuration.Get<Data>();
            Console.WriteLine(data.Bytes?.Length);
        }

        public class Data
        {
            public byte[] Bytes { get; set; }
        }

        public class Base64ByteArrayConverter : TypeConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                if (sourceType == typeof(string))
                {
                    return true;
                }
                return base.CanConvertFrom(context, sourceType);
            }

            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                if (value is string base64)
                {
                    return Convert.FromBase64String(base64);
                }
                return base.ConvertFrom(context, culture, value);
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                if (destinationType == typeof(byte[]))
                {
                    return true;
                }
                return base.CanConvertTo(context, destinationType);
            }

            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (value is byte[] bytes)
                {
                    return Convert.ToBase64String(bytes);
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }
}

/cc @maryamariyan @safern

@maryamariyan
Copy link
Member

Fixed by PR #43150

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@eiriktsarpalis eiriktsarpalis added question Answer questions and provide assistance, not an issue with source code or documentation. and removed customer assistance labels Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

5 participants