main / Source / WebClient / Web.Common / ConfigReader.cs

using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace DataUp.Web.Common
{
    /// <summary>
    /// Helper class for reading a value from Application Configuration file.
    /// </summary>
    /// <typeparam name="T">
    /// Type parameter.
    /// </typeparam>
    public static class ConfigReader<T>
    {
        /// <summary>
        ///  Helper method to read a value from config file. Returns user specified default value
        ///  in case any failure reading the config value.
        /// </summary>
        /// <param name="configKey">Configuration key name.</param>
        /// <param name="defaultValue">Default value to bet set in case of failure.</param>
        /// <returns>
        /// A valid value from configuration file.
        /// </returns>
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Avoids a new on every call"), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Need to ignore any exception.")]
        public static T GetConfigSetting(string configKey, T defaultValue)
        {
            try
            {
                string value = ConfigurationManager.AppSettings[configKey];
                if (!string.IsNullOrEmpty(value))
                {
                    return (T)Convert.ChangeType(value, defaultValue.GetType(), CultureInfo.InvariantCulture);
                }
            }
            catch (Exception)
            {
                // -
                // Swallow the exception to return a default value
                // -
            }

            return defaultValue;
        }

        /// <summary>
        ///  Helper method to read a value from config file (application config or cloud config file). 
        /// </summary>
        /// <param name="settingKey">Configuration key name.</param>
        /// <returns>
        /// A valid value from configuration file (application config or cloud config file).
        /// </returns>
        //// [WindowsAzureHostingPermission(SecurityAction.LinkDemand, Unrestricted = true)] 
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Avoids a new on every call"), SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "CA2122 conflicts with CA2135.")]
        public static T GetSetting(string settingKey)
        {
            return GetSetting(settingKey, default(T));
        }

        /// <summary>
        ///  Helper method to read a value from config file (application config or cloud config file). 
        /// </summary>
        /// <param name="settingKey">Configuration key name.</param>
        /// <param name="defaultValue">Default value to bet set in case of failure.</param>
        /// <returns>
        /// A valid value from configuration file (application config or cloud config file).
        /// </returns>
        //// [WindowsAzureHostingPermission(SecurityAction.LinkDemand, Unrestricted = true)] 
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Avoids a new on every call"), SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "CA2122 conflicts with CA2135.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Need to ignore any exception.")]
        public static T GetSetting(string settingKey, T defaultValue)
        {
            try
            {
                string settingValue = string.Empty;
                if (RoleEnvironment.IsAvailable)
                {
                    settingValue = RoleEnvironment.GetConfigurationSettingValue(settingKey);
                }
                else
                {
                    settingValue = ConfigurationManager.AppSettings[settingKey];
                }

                if (!string.IsNullOrEmpty(settingValue))
                {
                    return (T)Convert.ChangeType(settingValue, typeof(T), CultureInfo.InvariantCulture);
                }
            }
            catch
            {
                // -
                // Swallow the exception to return a default value
                // -
            }

            return defaultValue;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.