AppSettings.Get() Not working for string arrays

If I add to appSettings.json:

"test" : "some value";

I can then get that value like this:

var test = AppSettings.Get<string>("test");

However, If I add:

"test2" : [ "val1", "val2", "val3" ]

Then the following doesn’t work:

var test2 = AppSettings.Get<string[]>("test2");

It seems impossible to get it to read a string array.

I am using asp.net core 2.1. I have tried version 5.4 and also tried 5.4.1 from myget. Same issue.

This is causing the new FacebookAuthProvider to not request correct permissions because this line doesn’t read the permissions:

appSettings.Get<string[]>("oauth.facebook.Permissions", TypeConstants.EmptyStringArray);

I have isolated issue in new project using dotnet new angular-spa project and trying to read string array. It wont read a json string array.

I’ve been going in circles on this for a while. Have I missed something or is this a bug?

Use AppSettings.GetList() API to get an array of items, the Get API is for a complex type in a single string value, e.g containing a comma delimited list

Can you give me an explicit example please because when I try to set this:

  "oauth.facebook.Permissions": "email,user_friends",

It generates exception:

An error occurred while starting the application.
Internal Server Error
An error occurred while starting the application.
ConfigurationErrorsException:
NullReferenceException: Object reference not set to an instance of an object.
ServiceStack.Text.TranslateListWithElements.TranslateToConvertibleGenericICollectionCache(object from, Type toInstanceOfType, Type fromElementType)

The oauth.facebook.Permissions setting had an invalid format. The value “email,user_friends” could not be cast to type System.String
ServiceStack.NetCoreAppSettings.Bind(IConfigurationSection config)

NullReferenceException: Object reference not set to an instance of an object.
ServiceStack.Text.TranslateListWithElements.TranslateToConvertibleGenericICollectionCache(object from, Type toInstanceOfType, Type fromElementType)
ServiceStack.AutoMappingUtils.ConvertTo(object from)
ServiceStack.NetCoreAppSettings.Bind(IConfigurationSection config)

Show raw exception details
System.NullReferenceException: Object reference not set to an instance of an object.
at ServiceStack.Text.TranslateListWithElements.TranslateToConvertibleGenericICollectionCache(Object from, Type toInstanceOfType, Type fromElementType)
at ServiceStack.AutoMappingUtils.ConvertTo[T](Object from)
at ServiceStack.NetCoreAppSettings.Bind[T](IConfigurationSection config)
ConfigurationErrorsException: The oauth.facebook.Permissions setting had an invalid format. The value “email,user_friends” could not be cast to type System.String
ServiceStack.NetCoreAppSettings.Bind(IConfigurationSection config)

NullReferenceException: Object reference not set to an instance of an object.
ServiceStack.Text.TranslateListWithElements.TranslateToConvertibleGenericICollectionCache(object from, Type toInstanceOfType, Type fromElementType)
ServiceStack.AutoMappingUtils.ConvertTo(object from)
ServiceStack.NetCoreAppSettings.Bind(IConfigurationSection config)

How exactly should it appear in appSettings.json?

If it’s in your appsettings.json like:

{
  "oauth.facebook.Permissions": [ "email", "user_location" ]
}

It can be read like:

var permissions = AppSettings.GetList("oauth.facebook.Permissions");

But I’ve just added support for deserializing a JSON array collection into an enumerable collection in this commit, so you can also do:

var permissions = AppSettings.Get<string[]>("oauth.facebook.Permissions");

This change is available from v5.4.1 that’s now available on MyGet.

Hi Mythz,

Many thaks for implementing that. Prior to commit what format was this line of code you wrote in FacebookAuthProvider expecting?

appSettings.Get<string[]>("oauth.facebook.Permissions", TypeConstants.EmptyStringArray);

Was meant to handle a comma delimited list in a single string value, but there was an issue with the ConvertTo API fallback in deserializing string values which was also resolved on MyGet.

1 Like