Although in name this question is similar to this and this, it's not.
I'm currently developing a library that may require some custom configuration depending on the user's desire.
I have created a custom configuration section, and everything works just fine.
However, when I was debugging I noticed that the configuration section constructor was being called twice. And that's not what I intended.
Digging deeper, I discovered that it occurred because, in order to access the configuration information from the library, I use the following method:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var section = config.GetSection("myConfigSection");
Prior this, the .NET Framework had initialized the configuration environment for the application running the library, and thus calling the constructor of the MyConfigSection
class.
My question is, how to access the already loaded information?
Why the class' constructor is being called twice
Because I don't want to reload everything again, as the above code does.
Edited to add
The constructor is called twice even changing the above code to:
var section = ConfigurationManager.GetSection("myConfigSection");
Edited to Clarify
This question is not about accessing MyConfigSection
, I access it just fine.
The question is about why class' constructor is being called twice.
A little bit more of clarification
If the class' constructor is being called twice, the loading process is occurring twice.
And I simply doesn't want this to happen. It's ridiculous.
And yes, I'm calling the static methods of the Configurationmanager
as per my first edit in this question.