-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Labels
area-Extensions-Configuration
question
Answer questions and provide assistance, not an issue with source code or documentation.
Milestone
Comments
Dotnet-GitSync-Bot
added
area-System.Configuration
untriaged
New issue has not been triaged by the area owner
labels
Jun 4, 2020
Tagging subscribers to this area: @safern |
ericstj
added
area-Extensions-Configuration
and removed
area-System.Configuration
labels
Jun 30, 2020
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
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
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 |
Fixed by PR #43150 |
ghost
locked as resolved and limited conversation to collaborators
Dec 8, 2020
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.
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.
The text was updated successfully, but these errors were encountered: