We are looking to move sensitive configuration settings for a legacy .net framework 4.8 application to be held in Azure Key Vault which we have used successfuly for .net 6, 7, and 8 for a while now.
Using the NuGet package Microsoft.Configuration.ConfigurationBuilders.Azure and retrieving the configuration settings from ConfigurationManager.AppSettings works fine, adding a subsequent retrieval from ConfigurationManager.ConnectingStrings can take about 30 seconds to retrieve the connection string
This was a new vanilla console application using .net framework 4.8. Added the NugGet package Microsoft.Configuration.ConfigurationBuilders.Azure (current version 3.0.0). I’ve upgraded all the related packages to use latest versions as some were marked as deprecated. I’ve signed in to Azure using the Azure Service Authentication option within Visual Studio 2022.
I have set up a couple of secrets in Key Vault named Test2 and MyConnection.
My App.config has the following related sections:
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="AzureKeyVault" vaultName="Some-KeyVault" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</builders>
</configBuilders>
<connectionStrings configBuilders="AzureKeyVault">
<add name="MyConnection" connectionString="Obtained from key vault" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings configBuilders="AzureKeyVault">
<add key="Test1" value="Test value 1" />
<add key="Test2" value="Should come from key vault" />
</appSettings>
The console app then uses those settings – I have added a stop watch to show the issue:
static void Main(string[] args)
{
Console.WriteLine("Reading configuration file");
Stopwatch watch = new Stopwatch();
watch.Start();
Console.WriteLine($"Test1 --> {ConfigurationManager.AppSettings["Test1"]}");
Console.WriteLine($"Test 1 completed after {watch.Elapsed}");
Console.WriteLine($"Test2 --> {ConfigurationManager.AppSettings["Test2"]}");
Console.WriteLine($"Test 2 completed after {watch.Elapsed}");
Console.WriteLine($"ConnectionString -->{ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString}");
Console.WriteLine($"Test 3 completed after {watch.Elapsed}");
}
If I run this application I get:
Reading configuration file
Test1 --> Test value 1
Test 1 completed after 00:00:03.8970375
Test2 --> Test 2 coming from key vault
Test 2 completed after 00:00:03.8972798
ConnectionString --> Server=(local);Database=SomeDatabase;User ID=SomeUser;password=SomePassword;
Test 3 completed after 00:00:34.2944765
So it took over 30 seconds to retrieve and output the connection string. Strangely if I change the order and retrieve the connection string first and then the app settings, the connection string is reasonably quick but the app settings takes around 30 seconds.
We know therefore that we can connect to key vault reasonable quickly and retrieve a setting. It is that reading from a 2nd section that causes a delay.
Can anyone shed some light on why this happens?
Geoff Lloyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.