0
votes

I moved from values from loca.settings.json to Azure Key Vault. I added the Azure Key Vault as a Connected Service.

I have a problem with binding sections.

I have a class

public class AzureStorageBlobSettings
{
   public string AccountName { get; set; }
   public string ConnectionString { get; set; }
}

In Startup class, I bind Azure Key Vaults values to AzureStorageBlobSettings class.

builder.Services.Configure<AzureStorageBlobSettings>(_configuration.GetSection("AzureStorageBlobSettings"));

In my Azure Function, I inject AzureStorageBlobSettings using the Options pattern.

private readonly AzureStorageBlobSettings _azureStorageBlobSettings;
readonly IConfiguration _configuration;

public UploadDocumentFunction(AzureStorageBlobOptionsTokenGenerator azureStorageBlobOptionsTokenGenerator, 
            IOptions<AzureStorageBlobSettings> azureStorageBlobSettings, 
            IConfiguration configuration)
        {
            _azureStorageBlobOptionsTokenGenerator = azureStorageBlobOptionsTokenGenerator;
            _configuration = configuration;
            _azureStorageBlobSettings = azureStorageBlobSettings.Value;
        }

In Azure Key Vault I added the following secrets:

enter image description here

I added two dashes so AzureStorageBlobSettings should be a section, but settings aren't being bound and _azureStorageBlobSettings values are null.

Connection to Azure Key Vault works fine because when I get only one value from IConfiguration it works, like

_configuration["AzureStorageBlobSettings:AccountName"]

The question is what I am doing wrong with the binding section from Azure Key Vault?

1

1 Answers

0
votes

If using a "section--property" naming structure isn't a must for you, try binding TOptions against the entire Configuration:

builder.Services.Configure<AzureStorageBlobSettings>(_configuration);

And then name the secrets to match the class properties: AccountName and ConnectionString. Matching config properties/secrets will get bound to the class when you inject it.

I've only used the options pattern a few times, but this is how I've done it and it's worked fine.