Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
panuoksala opened this issue Apr 10, 2023 · 2 comments
Open

Comments

@panuoksala
Copy link

panuoksala commented Apr 10, 2023

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
Copy link
Author

panuoksala commented Apr 11, 2023

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants