0

I have a .NET 8 application where I read in values from an Azure Keyvault and then bind to configuration:

builder.Configuration.AddAzureKeyVault(new Uri($"https://mykeyvault.vault.azure.net/"),
            new DefaultAzureCredential()
        );

I then try and bind this configuration:

builder.Services.Configure<Settings>(builder.Configuration)
    .AddOptionsWithValidateOnStart<Settings>()
    .ValidateDataAnnotations();

In my Settings class, I have a property which is a string array:

public string[] MyArrayProperty { get; set; }

The value in the keyvault is a comma separated list:

"USD,EUR,GBP,CAD,JPY"

When I try and bind this property it is returned as null - I understand that my property is expected a string array and I am passing it a string.

How can I convert this string to an array when the binding process takes place?

1 Answer 1

0

There's a way to specify array settings in app configuration in ASP.NET Core/Azure Functions.

The way to specify config is (i present two ways):

"ArrayTest": {
  "FlatArray": [ "a", "b", "c" ],
  "Indexed:0": "a",
  "Indexed:1": "b",
  "Indexed:2": "c"
}

Then i tested it by using options pattern:

public class ArrayTest
{
    public string[] FlatArray { get; set; }

    public string[] Indexed { get; set; }
}

then configured it:

builder.Services.Configure<ArrayTest>(builder.Configuration.GetSection("ArrayTest"));

Then i could see both FlatArray and Indexed array filled correctly.

So, in order to do this in Azure Key Vault, you could see the second convention with indexes within the keys:

"Indexed:0": "a",
"Indexed:1": "b",
"Indexed:2": "c"

In order to add new item to array in config, you just add new Indexed:n key with value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.