ConfigurationManager and 2.0

B

Brent Dunham

I'm developing a class library that uses the ConfigurationManager for
applicationSettings. I created a Setting Class deriving from
ConfigurationSection, added properties nad attributes to reflect the schema.
All is good on adding the new section, except that when the Section is
detected as not there and needs to be added, I need to grab the default
settings. This is not happening. The default settings ARE saved to the
config file, however the Configuration object returned has null values(not
default) unless I initaliize them during declaration. This is an issue as I
do not want to set the DefaultValue parameter of the ConfigurationProperty
attribute with a different literal than the initalization. What I'm hoping
is the when the Configuration.Save() method is called, I would reload the
Configuration object with a call to
ConfigurationManager.OpenExeConfiguration() This doesn't do the trick. All
properties are null rather than populated with default values I can see have
been written to the app.config.

Does anyone have any info on how to achieve this?

thanks,

Brent
 
B

Brent Dunham

Anote to all who read this:

I discovered the problem while playing around. It seems that containing a
member variable to store the setting value isn't supported. I instead used
the property bag of the Configuration object to store and retrieve the
value.

Example:

Orignally I had this

private string _SettingName;

[ConfigurationProperty("SettingName", IsRequired = true,DefaultValue=
@"DefaultValue")]

public string BosJobQueueLocation

{

get { return _SettingName; }

set { _SettingName = value; }

}

But this is produced the results I was hoping for

[ConfigurationProperty("SettingName", IsRequired = true,DefaultValue=
@"DefaultValue")]

public string BosJobQueueLocation

{

get { return (string)this["SettingName"]; }

set { this["SettingName"] = value; }

}

I couldn't find anything on any blog about this so I thought it might be
helpful.

Thanks,

Brent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top