0

Currently, having some issues with setting this up properly. I have done a lot of reading on this on the web and tried multiple configurations with different code but for the following custom xml tags I cannot seem to get it working correctly. Can someone provide some feedback on how to tackle this? I want to access this outside from some other class and iterate through each "trackInfo" tags looking for a certain key. The last snippet of code is where I am trying to extract the elements and save them as variables

Things I am looking for: - what am I missing? - how do i access in an outside class?

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="trackLog" type="Tracking.TrackConfigSection,   Tracking" />
  </configSections>

  <trackLog>
    <trackInfo key="DEV1" env="dev1" url="http://dev1.com" />
    <trackInfo key="DEV2" env="dev2" url="http://dev2.com" />
  </trackLog>
</configuration>

TrackConfigSection.cs

public class TrackConfigSection : ConfigurationSection
{
    private const string SectionName = "trackLog";

    public TrackConfigSection()
    {
        base[""] = new TrackConfigCollection();
    }

    [ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)]
    public TrackConfigCollection TrackConfigs
    {
                get { return ((TrackConfigCollection)(base[""])); }
                set { base[""] = value; }
    }

    public static TrackConfigCollection GetTrackConfigs()
    {
        return ((TrackConfigSection)ConfigurationManager.GetSection(SectionName)).TrackConfigs;
    }
}

TrackConfigCollection.cs

[ConfigurationCollection(typeof(TrackConfigEntry), CollectionType = ConfigurationElementCollectionType.BasicMap)] 
        public class TrackConfigCollection : ConfigurationElementCollection
        {
            const string ItemName = "trackInfo";

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

            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((TrackConfigEntry)element).Key;
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new TrackConfigEntry();
            }

            public new TrackConfigEntry this[string key]
            {
                get 
                { 
                    return (TrackConfigEntry)BaseGet(key); 
                }
            }

            public bool Contains(string role)
            {
                return BaseGetAllKeys().Contains(role);
            }

        }

TrackConfigEntry.cs

public class TrackConfigEntry : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key 
    { 
        get { return (string)base["key"]; } 
    }

    [ConfigurationProperty("env", IsRequired = true)]
    public string env 
    { 
        get { return (string)base["env"]; } 
    }

    [ConfigurationProperty("url", IsRequired = true)]
    public string url 
    { 
        get { return (string)base["url"]; } 
    }
}

Accessing from some other class

var envName = System.Configuration.ConfigurationManager.AppSettings["env"];
// the envName is in the tags in the app.config
if (TrackConfigSection.GetTrackConfigs().Contains(envStringName))
{
string temp1 = TrackConfigSection.GetTrackConfig("key").ToString(); // variable to get key
                    string temp2 = TrackConfigSection.GetTrackConfig("env").ToString(); // variable to get env
                    string temp3 = TrackConfigSection.GetTrackConfig("url").ToString(); // variable to get url
}
5
  • 1
    Is the type correct? That doesn't seem to be a right assembly/type name pattern. Commented Jun 11, 2014 at 13:15
  • are you referring to the "<section name ..." ? Commented Jun 11, 2014 at 13:27
  • well i have a "Tracking" namespace with other classes using it too Commented Jun 11, 2014 at 13:33
  • is that the right assembly name? Commented Jun 11, 2014 at 13:35
  • ie, instead of type="Tracking.TrackConfigSection, TrackConfigSection" it should be type="Tracking.TrackConfigSection,name_of_assembly" where name of assembly is the assembly in which TrackConfigSection lives without the .dll at the end Commented Jun 11, 2014 at 13:37

1 Answer 1

1

Apart from the comment from @wal about fixing the assembly name in the configSection. You should also override ElementName property in your TrackConfigCollection class. Add the following to your TrackConfigCollection class:

protected override string ElementName
{
    get { return ItemName; }
}

Refer to this excellent article for more reading on custom Configuration

EDIT

var key = "DEV1"; 
if(TrackConfigSection.GetTrackConfigs().Contains(key))
{
    var config = TrackConfigSection.GetTrackConfigs()[key];
    var env = config.env;
    var url = config.url;
}
Sign up to request clarification or add additional context in comments.

4 Comments

and it still doesn't work? You getting any exceptions?
correct, it still does not work, does that snippet of code posted at the end of my question seem like it works?
it does seem to work for me. Code from my sample: var env = "DEV1"; var config = TrackConfigSection.GetTrackConfigs().Contains(env);
Yes that if statement works, but how do I go about extracting the "env" and the "url" for the tag that has the key="DEV1"? (like the correct functions to use)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.