views:

82

answers:

3

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.

A: 

If the configuration is already loaded by the parent application, you should be able to use the static GetSection right out of the block:

ConfigurationManager.GetSection('myConfigSection');

If that's not what you wanted, maybe you can better explain your situation. You might also be interested in the AppSettings - http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Chad
To clarify: The question is about WHY the class' constructor is being called twice.
Paulo Santos
A: 

You asked two questions: Why is the ctor being called twice, and how do you access the already-loaded config.

You already pointed out why it's being called twice. The configuration system is parsing the config file when the app is loaded. It makes that config available via ConfigurationManager's static members, including ConfigurationManager.AppSetting and ConfigurationManager.GetSection().

You access the already-loaded config by using these static members.

gWiz
A: 

After further investigation, the constructor is being called twice because of the following circumstance:

  1. The .NET Framework creates the class when it finds the <section name="..." type="..." /> in the <configSections> element.
  2. If there's a section configured in the app.config or web.config the .NET Framework, creates another instance of the specified class to decode the section and merge with the already created instance.

Although it does work according to the documentation, this behavior might clash with the implementation of a ConfigurationSection if the developer thinks that only one object is created during the lifetime of the application.

So, following this train of thought, if the configuration is specified in, say, the machine.config, app.config and user.config the object will be constructed three times in order to merge everything together.

Paulo Santos