-3

I need to create nested sections in my Web.config file, i can't find any examples that match my requirements.

<IPTests>
    <Environment environment="DEV">
      <Machine machine="Web01">
        <SiteIP site="Sitecore" ip="10.10.2.191">
        </SiteIP>
      </Machine>
    </Environemnt>
</IPTests>

This is used for a "Health Check" for different applications/sites. I need to check all of the resources that different sites use are working. I've done this already using DNS, however i now need to do it in our different environments for our different servers by hitting the different servers with an ip address.

Any help would be great!

This is what i have thus far.

public class IPTests : ConfigurationSectionGroup
    {
        [ConfigurationProperty("codeEnvironment")]
        public CodeEnvironmentSection CodeEnvironment
        {
            get { return (CodeEnvironmentSection)base.Sections["codeEnvironment"]; }
        }
    }

    public class CodeEnvironmentSection : ConfigurationSection
    {
        [ConfigurationProperty("environemnt")]
        public ValueElement To
        {
            get { return (ValueElement)base["environemnt"]; }
        }
    }

    public class MachineSection : ConfigurationSection
    {
        [ConfigurationProperty("machine")]
        public ValueElement To
        {
            get { return (ValueElement)base["machine"]; }
        }
    }

    public class SiteIPSection : ConfigurationSection
    {
        [ConfigurationProperty("site")]
        public ValueElement To
        {
            get { return (ValueElement)base["site"]; }
        }

        [ConfigurationProperty("ip")]
        public ValueElement To
        {
            get { return (ValueElement)base["ip"]; }
        }
    }

    public class ValueElement : ConfigurationElement
    {
        [ConfigurationProperty("value")]
        public string Value
        {
            get { return (string)base["value"]; }
            set { base["value"] = value; }
        }
    }
Staleyr
  • 643
  • 4
  • 8
  • 12
  • 5
    [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – Darin Dimitrov Jul 10 '12 at 18:09
  • i've been trying to use this previous example: http://stackoverflow.com/questions/5027284/nested-configuration-section-app-config however i can't get it to work becuase of my multiple nests – Staleyr Jul 10 '12 at 18:17

2 Answers2

0

Have a look at the Configuration Section Designer. I used it to generate configuration classes and then spent some time understanding the APIs and design.

bloudraak
  • 5,902
  • 5
  • 37
  • 52
0

It looks like you're trying to store a database of information in your web.config. If this is likely to change at all then you shouldn't store it here.

The problem is that any change to the web.config will cause your website to restart and reset the users session cache, etc.

If you are committed to adding your own sections then you can do this by creating a custom ConfigurationSection class:

You can search the web for more info on this as there are quite a few tutorials out there.

rtpHarry
  • 13,019
  • 4
  • 43
  • 64
  • i'm running resource tests on different services, databases, folders ect. and i need to run these tests on different ips (it's a health check test if you will). And the information doesn't change that often, but if it does it needs to be easily changed, that's why it's in the web.config file. – Staleyr Jul 10 '12 at 18:25
  • fair enough, you could just put it in an xml file which easily supports nested elements - depends how annoyed you are with the `ConfigurationSection` class yet :) – rtpHarry Jul 10 '12 at 18:42
  • very very annoyed lol, but i'm just a newbie intern, and this is how my supervisor wants it :/ – Staleyr Jul 10 '12 at 18:48