.NET Custom Settings

We normally tend to lazy and use the .NET Configuration AppSettings element for defining our own custom configuration keys. Though this does solve the purpose but sometime we need MORE. We may need our own tags like <myTag> to be used in the configuration file. .NET provides a standard mechansim to achieve this. Lets assume we want the following in our .config file

<MySettings>

    <Setting Name=”” Value=”” IsValid=”” />

</MySettings>

There are 5 things to be done

  1. Define a class that inherits from ConfigurationElement, this represent the element node i.e. <MySetting> for the above mentioned XML.
  2. Define a class that inherits from ConfigurationElementCollection, this represents the collection node that contain the element node i.e. <MySettings> for the above mentioned XML.
  3. Define a class that inherits from ConfigurationSection, this represent the master class through which the section is accessed.
  4. Add entry in the <configSections> of the config file.
  5. Use System.Configuration.ConfigurationManager.GetSection method to access the section defined in the code.

Now let’s drill with the code

Inherit from ConfigurationElement

public class MySetting : ConfigurationElement
    {
                
        [ConfigurationProperty(“Name”, IsRequired = true)]
        public String Name
        {
            get
            {
                return (string)this[“Name”];
            }
            set
            {
                this[“Name”] = value;
            }
        }

      
        [ConfigurationProperty(“Value”)]
        public String Value
        {
            get
            {
                return (string)this[“Value”];
            }
            set
            {
                this[“Value”] = value;
            }
        }

        [ConfigurationProperty(“IsValid”)]
        public bool IsValid
        {
            get
            {
                return (bool)this[“IsValid”];
            }
            set
            {
                this[“IsValid”] = value;
            }
        }
    }

Inherit from ConfigurationElementCollection

public class MySettings : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MySetting();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MySetting)element).Name;
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
        protected override string ElementName
        {
            get { return “MySetting”; }
        }
        public MySetting this[int index]
        {
            get{ return (MySetting)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }
        new public MySetting this[string Name]
        {
            get { return (MySetting)BaseGet(Name); }
        }
        public bool ContainsKey(string key)
        {
            bool result = false;
            object[] keys = BaseGetAllKeys();
            foreach (object obj in keys)
            {
                if ((string)obj == key)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }

Inherit from ConfigurationSection

public class MySection:ConfigurationSection
    {
       [ConfigurationProperty(“MySettings”,IsDefaultCollection=true)]
        public MySettings MySetting
        {
            get
            {
                return (MySettings)base[“MySettings”];
            }
        }
    }

 

Entry in App.Config/Web.Config

<configuration>
  <configSections>
    <section name=”MySettings” type=”DebasishPramanik.Configuration.MySection,
      CustomSettings”/>
  </configSections>
  <MySettings>
    <MySetting Name=”BlogID” Value=”12345″ IsValid=”true”/>
  </MySettings>
</configuration>

 

Use in Application

 

DebasishPramanik.Configuration.MySection section = (DebasishPramanik.Configuration.MySection)System.Configuration.ConfigurationManager.GetSection("MySettings");
            
foreach (DebasishPramanik.Configuration.Setting setting in section.MySettings)
 {
     Console.WriteLine(setting.Name);
}

 

 

    Advertisements

    Remove Cookie in ASP.NET

    I was working on the Cookies using the HTTPCookie class in ASP.NET. Though it is simple to add cookie and use them in the program but I really struggle to remove the cookies. I tried initially

    1. Get the reference to the cookie.
    2. Use the Response.Cookies.Remove method to remove the cookie.

    I expected the API to behave as per the name i.e. delete the cookie but to my utter shock it didn’t work. After long search I found the solution. The key was to make the cookie expire instead of removing it from the collection

    Response.Cookies[“MY_COOKIE”].Expires = DateTime.Now;

    This works perfectly for me.