PraxisWeb
asked on
Using Custom Configuration Sections in ASP.NET (2.0)
I have started playing around with the 2.0 custom configuration settings and am pounding my head on the keyboard trying to figure out why this isn't working...
============= WEB.CONFIG (partial)================
<configSections>
<section name="servers" type="Dashboard.Config.Ser versSectio n" />
</configSections>
<appSettings>
<add key="TimerInterval" value="3000"/>
</appSettings>
<servers>
<server name="BES" />
<server name="SDA" />
</servers>
<connectionStrings/>
<system.web>
...
</system.web>
-------------------------- ---------- ---------- ---------- ---------- ---
============= serverConfig.cs ===============
namespace Dashboard.Config {
using System;
using System.Configuration;
public class ServerElement : System.Configuration.Confi gurationEl ement
{
[System.Configuration.Conf igurationP roperty("n ame", DefaultValue = "SDA", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return ((string)(base["name"]));
}
set
{
base["name"] = value;
}
}
}
[System.Configuration.Conf igurationC ollectionA ttribute(t ypeof(Serv erElement) )]
public class ServerElementCollection : System.Configuration.Confi gurationEl ementColle ction
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
public override ConfigurationElementCollec tionType CollectionType
{
get { return ConfigurationElementCollec tionType.B asicMap; }
}
protected override object GetElementKey(Configuratio nElement element)
{
return ((ServerElement)element).N ame;
}
protected override string ElementName
{
get { return "server"; }
}
public new int Count
{
get { return base.Count; }
}
public ServerElement this[int index]
{
get { return (ServerElement)BaseGet(ind ex); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ServerElement this[string Name]
{
get
{
return (ServerElement)BaseGet(Nam e);
}
}
public int IndexOf(ServerElement server)
{
return BaseIndexOf(server);
}
}
public class ServersSection : System.Configuration.Confi gurationSe ction
{
[System.Configuration.Conf igurationP roperty("s erver")]
public ServerElementCollection Servers
{
get { return ((ServerElementCollection) (base["ser ver"])); }
}
}
}
-------------------------- ---------- ---------- ---------- ---------- --------
=============== TestPage.aspx.cs ================
using .... (Standard page namespaces)
using Dashboard.Config
public partial class _TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServersSection serversSection =
(ServersSection)Configurat ionManager .GetSectio n("servers ");
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------
When running this I get the following exception thrown:
Unrecognized attribute 'name'. Note that attribute names are case-sensitive.
For the life of me I'm not sure why - the MSDN examples are pretty sparse.
Thanks,
-Jason
============= WEB.CONFIG (partial)================
<configSections>
<section name="servers" type="Dashboard.Config.Ser
</configSections>
<appSettings>
<add key="TimerInterval" value="3000"/>
</appSettings>
<servers>
<server name="BES" />
<server name="SDA" />
</servers>
<connectionStrings/>
<system.web>
...
</system.web>
--------------------------
============= serverConfig.cs ===============
namespace Dashboard.Config {
using System;
using System.Configuration;
public class ServerElement : System.Configuration.Confi
{
[System.Configuration.Conf
public string Name
{
get
{
return ((string)(base["name"]));
}
set
{
base["name"] = value;
}
}
}
[System.Configuration.Conf
public class ServerElementCollection : System.Configuration.Confi
{
protected override ConfigurationElement CreateNewElement()
{
return new ServerElement();
}
public override ConfigurationElementCollec
{
get { return ConfigurationElementCollec
}
protected override object GetElementKey(Configuratio
{
return ((ServerElement)element).N
}
protected override string ElementName
{
get { return "server"; }
}
public new int Count
{
get { return base.Count; }
}
public ServerElement this[int index]
{
get { return (ServerElement)BaseGet(ind
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ServerElement this[string Name]
{
get
{
return (ServerElement)BaseGet(Nam
}
}
public int IndexOf(ServerElement server)
{
return BaseIndexOf(server);
}
}
public class ServersSection : System.Configuration.Confi
{
[System.Configuration.Conf
public ServerElementCollection Servers
{
get { return ((ServerElementCollection)
}
}
}
--------------------------
=============== TestPage.aspx.cs ================
using .... (Standard page namespaces)
using Dashboard.Config
public partial class _TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServersSection serversSection =
(ServersSection)Configurat
}
}
--------------------------
When running this I get the following exception thrown:
Unrecognized attribute 'name'. Note that attribute names are case-sensitive.
For the life of me I'm not sure why - the MSDN examples are pretty sparse.
Thanks,
-Jason
Hi Praxis,
The XML should look like this in Web.Config.
-------------------------- ---------- ---------- ---------- ---------- ---
<configuration>
<configSections>
<sectionGroup name="Axiom4366">
<section name="PCOEAxiomIsOrderCanc elEligible " type="System.Configuration .NameValue SectionHan dler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c56193 4e089" />
</sectionGroup>
</configSections>
<Axiom4366>
<PCOEAxiomIsOrderCancelEli gible>
<add key="Xpath" value="OMAPIForIEFW/isOrde rCancelEli gible"></a dd>
<add key="CCASystemRef" value="CCAExecutionContext ID"></add>
</PCOEAxiomIsOrderCancelEl igible>
</Axiom4366>
</configuration>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
Note -
1. u should use only Key and Value pair in Custom sections to make life simple.
2. Following attribute values should taken form ur machine.config
Version=1.0.5000.0,
Culture=neutral,
PublicKeyToken=b77a5c56193 4e089
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
Then use following code to access it
NameValueCollection oID=(NameValueCollection )ConfigurationSettings.Get Config("Ax iom4366/PC OEAxiomIsO rderCancel Eligible") ;
Then to get the values as required from it use.
String sXPath = oID["Xpath"];
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
In ur case, use like following
<servers>
<server Key = "name" value = "BES, SDA" />
</servers>
Then once u get the Names list, split it on "," to get required list. :-)
Anand
C, its simple like that.
The XML should look like this in Web.Config.
--------------------------
<configuration>
<configSections>
<sectionGroup name="Axiom4366">
<section name="PCOEAxiomIsOrderCanc
</sectionGroup>
</configSections>
<Axiom4366>
<PCOEAxiomIsOrderCancelEli
<add key="Xpath" value="OMAPIForIEFW/isOrde
<add key="CCASystemRef" value="CCAExecutionContext
</PCOEAxiomIsOrderCancelEl
</Axiom4366>
</configuration>
--------------------------
Note -
1. u should use only Key and Value pair in Custom sections to make life simple.
2. Following attribute values should taken form ur machine.config
Version=1.0.5000.0,
Culture=neutral,
PublicKeyToken=b77a5c56193
--------------------------
Then use following code to access it
NameValueCollection oID=(NameValueCollection )ConfigurationSettings.Get
Then to get the values as required from it use.
String sXPath = oID["Xpath"];
--------------------------
In ur case, use like following
<servers>
<server Key = "name" value = "BES, SDA" />
</servers>
Then once u get the Names list, split it on "," to get required list. :-)
Anand
C, its simple like that.
ASKER
anand2k, Thanks but I can do that just fine, the problem is I have a much more complicated config section and need to get at least this part working as defined above - I have all the code for the below in place (just removed it from my above post for the sake of simplicy and size). If I can't get past the name attribute on just server then the rest of this is moot.
-Jason
The full config is:
<servers>
<server name="FOO" enabled="true">
<connect address="127.0.0.1" domain="****" username="****" password="******"/>
<servicemonitors>
<clear/>
<add name="ASF Agent" required="false"/>
<add name="OracleOraHome90TNSLi stener" required="true"/>
<add name="Performance Logs and Alerts" required="false"/>
</servicemonitors>
<performancemonitors>
<clear/>
<add category="Processor" counter="% Processor Time" instance="_Total" format="N2"
low="0" high="85" convert="" />
<add category="System" counter="System Up Time" instance="" format="N0"
low="0" high="-1" />
<add category="Memory" counter="% Committed Bytes In Use" instance="" format="N2"
low="0" high="85" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="LogicalDisk" counter="Free Megabytes" instance="C:" format="N2"
low="5400" high="-1" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
</performancemonitors>
</server>
<server name="BAR" enabled="false">
<connect address="XXX.XXX.XXX.XXX" domain="****" username="******" password="********"/>
<servicemonitors>
<clear/>
<add name="Messenger" required="false"/>
<add name="SQL%" required="true"/>
</servicemonitors>
<performancemonitors>
<clear/>
<add category="Processor" counter="% Processor Time" instance="_Total" format="N2"
low="0" high="85" convert="" />
<add category="System" counter="System Up Time" instance="" format="N0"
low="0" high="-1" />
<add category="Memory" counter="% Committed Bytes In Use" instance="" format="N2"
low="0" high="85" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="LogicalDisk" counter="Free Megabytes" instance="C:" format="N2"
low="5400" high="-1" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="SDA System" counter="Queued Service Orders records" instance=""
format="N0" low="0" high="2"/>
</performancemonitors>
</server>
</servers>
-Jason
The full config is:
<servers>
<server name="FOO" enabled="true">
<connect address="127.0.0.1" domain="****" username="****" password="******"/>
<servicemonitors>
<clear/>
<add name="ASF Agent" required="false"/>
<add name="OracleOraHome90TNSLi
<add name="Performance Logs and Alerts" required="false"/>
</servicemonitors>
<performancemonitors>
<clear/>
<add category="Processor" counter="% Processor Time" instance="_Total" format="N2"
low="0" high="85" convert="" />
<add category="System" counter="System Up Time" instance="" format="N0"
low="0" high="-1" />
<add category="Memory" counter="% Committed Bytes In Use" instance="" format="N2"
low="0" high="85" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="LogicalDisk" counter="Free Megabytes" instance="C:" format="N2"
low="5400" high="-1" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
</performancemonitors>
</server>
<server name="BAR" enabled="false">
<connect address="XXX.XXX.XXX.XXX" domain="****" username="******" password="********"/>
<servicemonitors>
<clear/>
<add name="Messenger" required="false"/>
<add name="SQL%" required="true"/>
</servicemonitors>
<performancemonitors>
<clear/>
<add category="Processor" counter="% Processor Time" instance="_Total" format="N2"
low="0" high="85" convert="" />
<add category="System" counter="System Up Time" instance="" format="N0"
low="0" high="-1" />
<add category="Memory" counter="% Committed Bytes In Use" instance="" format="N2"
low="0" high="85" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="LogicalDisk" counter="Free Megabytes" instance="C:" format="N2"
low="5400" high="-1" />
<add category="Memory" counter="Available KBytes" instance="" format="N0"
low="298000" high="-1" />
<add category="SDA System" counter="Queued Service Orders records" instance=""
format="N0" low="0" high="2"/>
</performancemonitors>
</server>
</servers>
ASKER
I discovered my own solution.
The SectionHandler root needs to have another collection container inside of it so I ended up changing my config file (and therefore code) to match the following:
<serverManager>
<servers>
<server name="FOO" enabled="true">
...
</server>
<server name="BAR" enabled="false">
...
</server>
</servers>
</serverManager>
Once I had a ConfigurationElementCollec tion wrapping the individual ConfigurationElements I was able to iterate through the rest of the xml with no problems.
***** Requesting a PAQ/refund *****
*see http:Q_21825078.html for details *
************************** ***
The SectionHandler root needs to have another collection container inside of it so I ended up changing my config file (and therefore code) to match the following:
<serverManager>
<servers>
<server name="FOO" enabled="true">
...
</server>
<server name="BAR" enabled="false">
...
</server>
</servers>
</serverManager>
Once I had a ConfigurationElementCollec
***** Requesting a PAQ/refund *****
*see http:Q_21825078.html for details *
**************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER