• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4501
  • Last Modified:

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.ServersSection" />
</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.ConfigurationElement
    {
        [System.Configuration.ConfigurationProperty("name", DefaultValue = "SDA", IsKey = true, IsRequired = true)]
        public string Name
        {
            get
            {
                return ((string)(base["name"]));
            }
            set
            {
                base["name"] = value;
            }
        }
    }

    [System.Configuration.ConfigurationCollectionAttribute(typeof(ServerElement))]
    public class ServerElementCollection : System.Configuration.ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServerElement();
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServerElement)element).Name;
        }

        protected override string ElementName
        {
            get { return "server"; }
        }

        public new int Count
        {
            get { return base.Count; }
        }

        public ServerElement this[int index]
        {
            get { return (ServerElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public ServerElement this[string Name]
        {
            get
            {
                return (ServerElement)BaseGet(Name);
            }
        }

        public int IndexOf(ServerElement server)
        {
            return BaseIndexOf(server);
        }

    }

    public class ServersSection : System.Configuration.ConfigurationSection
    {
        [System.Configuration.ConfigurationProperty("server")]
        public ServerElementCollection Servers
        {
            get { return ((ServerElementCollection)(base["server"])); }
        }
    }
}
--------------------------------------------------------------------------
=============== 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)ConfigurationManager.GetSection("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
0
PraxisWeb
Asked:
PraxisWeb
  • 3
1 Solution
 
PraxisWebAuthor Commented:
I have also placed a pointer to this Question in ASP.NET TA
0
 
anand2kCommented:
Hi Praxis,

The XML should look like this in Web.Config.
---------------------------------------------------------------------
<configuration>
      <configSections>
            <sectionGroup name="Axiom4366">
                  <section name="PCOEAxiomIsOrderCancelEligible" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </sectionGroup>
      </configSections>
      <Axiom4366>
            <PCOEAxiomIsOrderCancelEligible>
                  <add key="Xpath" value="OMAPIForIEFW/isOrderCancelEligible"></add>
                  <add key="CCASystemRef" value="CCAExecutionContextID"></add>
            </PCOEAxiomIsOrderCancelEligible>
      </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=b77a5c561934e089
--------------------------------------------------------------------------------------------

Then use following code to access it

NameValueCollection oID=(NameValueCollection )ConfigurationSettings.GetConfig("Axiom4366/PCOEAxiomIsOrderCancelEligible");


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.
0
 
PraxisWebAuthor Commented:
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="OracleOraHome90TNSListener" 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>
0
 
PraxisWebAuthor Commented:
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 ConfigurationElementCollection 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 *
*****************************
0
 
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now