Is there a way to add a list or array to an appsettings key?
I want to maintain a list of servers that the app should query, configurable on the fly.
Any ideas on the best method to accomplish this?
-
-
The best way to do this would be to define your own configuration element, which is remarkably easy with the declarative syntax of .Net 2.0. For example:
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey=true, IsRequired=true)]
public string Name
{
get { return (string)this["name"] }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElement)element).Name;
}
}
class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
}
Then you can use it in your app.config file:
<configuration>
<configSections>
<section name="mycompany.mysection" type="MyNamespace.MyConfigurationSection, MyAssembly" requirePermission="false" />
</configSections>
<mycompany.mysection>
<servers>
<add name="server1" />
<add name="server2" />
</servers>
</mycompany.mysection>
</configuration>
And from code you can access the section like this:
MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection"); -
Sven Groot wrote:
The best way to do this would be to define your own configuration element, which is remarkably easy with the declarative syntax of .Net 2.0. For example:
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey=true, IsRequired=true)]
public string Name
{
get { return (string)this["name"] }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElement)element).Name;
}
}
class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
}
Then you can use it in your app.config file:
<configuration>
<configSections>
<section name="mycompany.mysection" type="MyNamespace.MyConfigurationSection, MyAssembly" requirePermission="false" />
</configSections>
<mycompany.mysection>
<servers>
<add name="server1" />
<add name="server2" />
</servers>
</mycompany.mysection>
</configuration>
And from code you can access the section like this:
MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection");
Wowzers, you're awesome. kudos! -
Dylan Copeland wrote:Yes, Mr Sven is awesome.
Don't call me "Mr Sven", it makes me feel old.
EDIT: Kids these days...
-
Sven Groot wrote:

Dylan Copeland wrote:
Yes, Mr Sven is awesome.
Don't call me "Mr Sven", it makes me feel old.
EDIT: Kids these days...
I meant to reply to this same thing yesterday.
Mr Phreaks would most likely be my father, not me. -
phreaks wrote:

Sven Groot wrote:

Dylan Copeland wrote:
Yes, Mr Sven is awesome.
Don't call me "Mr Sven", it makes me feel old.
EDIT: Kids these days...
I meant to reply to this same thing yesterday.
Mr Phreaks would most likely be my father, not me.
Well, obviously my dad would be Mr Groot, not Mr Sven. But still.
-
Sven Groot wrote:
The best way to do this would be to define your own configuration element, which is remarkably easy with the declarative syntax of .Net 2.0. For example:
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey=true, IsRequired=true)]
public string Name
{
get { return (string)this["name"] }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElement)element).Name;
}
}
class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
}
Then you can use it in your app.config file:
<configuration>
<configSections>
<section name="mycompany.mysection" type="MyNamespace.MyConfigurationSection, MyAssembly" requirePermission="false" />
</configSections>
<mycompany.mysection>
<servers>
<add name="server1" />
<add name="server2" />
</servers>
</mycompany.mysection>
</configuration>
And from code you can access the section like this:
MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection");
That is really great, thanks.
Can I add values to the keys, like maybe a port number?
<servers>
<add name="server1" tcpport="8092" />
<add name="server2" tcpport="8093" />
</servers>
-
Sure, just add an extra property to the ServerElement class:
[ConfigurationProperty("port")]
public int Port
{
get { return (int)this["port"]; }
}
Note that since it isn't part of the element's key, you wouldn't be able to add the same server twice with different port numbers this way. If you want to do that you'd better just add the port to the server name using the standard server:port notation (e.g. server1:8092). -
Sven Groot wrote:Sure, just add an extra property to the ServerElement class:
[ConfigurationProperty("port")]
public int Port
{
get { return (int)this["port"]; }
}
Note that since it isn't part of the element's key, you wouldn't be able to add the same server twice with different port numbers this way. If you want to do that you'd better just add the port to the server name using the standard server:port notation (e.g. server1:8092).
Excellent, thank you very much. -
one last question...
What does MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection"); return?
I thought it should return a ServerElementCollection
but I am having a hard time iterating it. -
phreaks wrote:one last question...
What does MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection"); return?
An instance of the MyConfigurationSection class filled with the values from the section with the specified name in the app.config file, or null if no such section exists.
You might want to consider reading the documentation on creating custom configuration sections.
-
Sven Groot wrote:

phreaks wrote:
one last question...
What does MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection"); return?
An instance of the MyConfigurationSection class filled with the values from the section with the specified name in the app.config file, or null if no such section exists.
You might want to consider reading the documentation on creating custom configuration sections.
Ya, I am right now.
Hehe, I think I did something wrong though...
{"An error occurred creating the configuration section handler for Avacado.MatLabServers: Could not load file or assembly 'MatLabQueuePollServiceLibrary.ConfigurationHandlers' or one of its dependencies. The system cannot find the file specified. (C:\\Users\\jandlynn\\Documents\\Visual Studio 2005\\Projects\\MatLabQueuePollServiceLibrary\\MatLabPollQueueForms\\bin\\Debug\\MatLabPollQueueForms.vshost.exe.config line 5)"} -
what is ServerConfigurationElement here?. I am getting error as "Inconsistent accessibility: property type 'ServerElementCollection' is less accessible than property 'MyConfigurationSection.Servers'"
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElement)element).Name;
}
}
public class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
} -
This is the most newbie friendly code. Thanx much.
-
Rashmi Ranjan Sinha wrote:what is ServerConfigurationElement here?. I am getting error as "Inconsistent accessibility: property type 'ServerElementCollection' is less accessible than property 'MyConfigurationSection.Servers'"
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElementServerElement)element).Name;
}
}
public class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
}
ServerConfigurationElement is a typo. Use ServerElement instead. -
Sven Groot said:
The best way to do this would be to define your own configuration element, which is remarkably easy with the declarative syntax of .Net 2.0. For example:
using System.Configuration;
class ServerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey=true, IsRequired=true)]
public string Name
{
get { return (string)this["name"] }
}
}
class ServerElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerConfigurationElement)element).Name;
}
}
class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("servers")]
public ServerElementCollection Servers
{
get { return (ServerElementCollection)this["servers"]; }
}
}
Then you can use it in your app.config file:
<configuration>
<configSections>
<section name="mycompany.mysection" type="MyNamespace.MyConfigurationSection, MyAssembly" requirePermission="false" />
</configSections>
<mycompany.mysection>
<servers>
<add name="server1" />
<add name="server2" />
</servers>
</mycompany.mysection>
</configuration>
And from code you can access the section like this:
MyConfigurationSection section = (MyConfigurationSection)ConfigurationManager.GetSection("mycompany.mysection");To get this working I had to add another property to the ServerElementCollection class, found the example on msdn at
http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx
public ServerElement this[int index]
{
get
{
return (ServerElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.