aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
118 stars 61 forks source link

Random not found error while trying to read app settings at constructor #222

Open panuoksala opened 1 year ago

panuoksala commented 1 year ago

We are using Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder in a .NET Framework 4.8 Web API project. Normally everything works, but sometimes (randomly) our application fails to get secret from KeyVault. I think this is related to constructor/static/locking, because it happens mostly at Controller constructor. For example we have a class that is registered into Autofac DI container and that class tries to read connectionstring from KeyVault at constructor. This class is constructured at API Controller constructor.

 var builder = new ServiceBusConnectionStringBuilder(ConfigurationManager.AppSettings["ServiceBusConnectionString"])
            {
                EntityPath = entityPath
            };

Sometimes that line creates following error:

An error occurred when trying to create a controller of type 'InvoiceController'. Make sure that the controller has a 
parameterless public constructor. An exception was thrown while activating API.Controllers.InvoiceController -> 
Common.Invoicing.CreateInvoicePdfService -> Integration.ServiceBus.Services.PaymentService. 
An exception was thrown while invoking the constructor 'Void .ctor()' on type 'PaymentService'. 
Value for the connection string parameter name 'key vault' was not found.
Parameter name: connectionString 

Error Call Stacks

Inner exception System.Exception handled at System.Configuration.BaseConfigurationRecord.EvaluateOne:
   at Microsoft.Configuration.ConfigurationBuilders.KeyValueConfigBuilder.EnsureGreedyInitialized (Microsoft.Configuration.ConfigurationBuilders.Base, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.Configuration.ConfigurationBuilders.KeyValueConfigBuilder.ProcessConfigurationSection (Microsoft.Configuration.ConfigurationBuilders.Base, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at System.Configuration.BaseConfigurationRecord.CallHostProcessConfigurationSection (System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
Inner exception System.NullReferenceException handled at Microsoft.Configuration.ConfigurationBuilders.KeyValueConfigBuilder.EnsureGreedyInitialized:
   at Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder.GetAllValues (Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at Microsoft.Configuration.ConfigurationBuilders.KeyValueConfigBuilder.EnsureGreedyInitialized (Microsoft.Configuration.ConfigurationBuilders.Base, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)

Could we call something at startup to ensure that everything is properly loaded, before starting to initialize DI container?

panuoksala commented 1 year ago

For more information: The app is hosted at Azure App Service and it has relatively high traffic (multiple calls/sec). When the app is starting it will most likely receive an immediate call that creates the controller. That's made me wonder about possible LazyInitialize problem and race condition.

We use "key vault" as value for app.config values that are stored into Azure KeyVault. Parameter name: connectionString refers to ServiceBusConnectionStringBuilder class from Microsoft.Azure.ServiceBus library.

StephenMolloy commented 1 year ago

Looks like you're still on the 2.0 config builders. This might be #140, which should be fixed in the 3.0 release.