Integrated Solutions, Inc.

Custom Configuration section

I HATE “ConfigurationSettings.AppSettings” !!!!

That being said, .NET gives us a great alternative that no one talks about, custom configuration sections. It gives us a fully nestable and strongly typed config section.

Add a new class:

using System;
using System.Configuration;

namespace ISI.HAPP.API
{
  public sealed partial class Configuration : ConfigurationSection
  {
    public class AuthenicationParameters : ConfigurationElement
    {
      #region UseNTPermissions
      /// <summary>
      /// Gets or sets the UseNTPermissions.
      /// </summary>
      [ConfigurationProperty("UseNTPermissions", IsRequired = true)]
      public bool UseNTPermissions
      {
        get
        {
          return (bool)base["UseNTPermissions"];
        }
        set
        {
          base["UseNTPermissions"] = value;
        }
      }
      #endregion

      #region DefaultNTDomain
      /// <summary>
      /// Gets or sets the DefaultNTDomain.
      /// </summary>
      [ConfigurationProperty("DefaultNTDomain", IsRequired = false)]
      public string DefaultNTDomain
      {
        get
        {
          return (string)base["DefaultNTDomain"];
        }
        set
        {
          base["DefaultNTDomain"] = value;
        }
      }
      #endregion
    }

    #region Current
    public static readonly Configuration Current;
    static Configuration()
    {
      const string ConfigurationSectionName = "ISI.HAPP.API.Configuration";

      Current = null;
      if (Current == null) Current = (Configuration)(System.Configuration.ConfigurationManager.GetSection(ConfigurationSectionName));
      if (Current == null) throw new Exception(string.Format("Missing {0} Configuration Section", ConfigurationSectionName));
    }
    #endregion

    public Configuration() : base() { }

    #region CompanyName
    [ConfigurationProperty("CompanyName", IsRequired = false)]     public string CompanyName
    {
      get
      {
        return (string)base["CompanyName"];
      }
      set
      {
        base["CompanyName"] = value;
      }
    }
    #endregion

    #region Authenication
    public AuthenicationParameters Authenication
    {
      get
      {
        return (AuthenicationParameters)base["Authenication"];
      }
    }
    #endregion
  }
}

and add the following to the “configSections” of your app/web.config:

    <section name="ISI.HAPP.API.Configuration" type="ISI.HAPP.API.Configuration, ISI.HAPP.API.Core" requirePermission="false"/>

and the following after the “configSections”

  <ISI.HAPP.API.Configuration>
    <CompanyName>ISI</CompanyName>
    <Authenication>
      <UseNTPermissions>true</UseNTPermissions>
      <DefaultNTDomain>ISI</DefaultNTDomain>
    </Authenication>
  </ISI.HAPP.API.Configuration>

 

It’s that simple …….

Published 06/19/2009 by Ron Muth


 
<script type="text/javascript"> jQuery(document).ready(function (jQuery) { ISI.Web.Interface.Areas.Blogs.Layout.Setup(); }); </script>