Helper methods for retrieving web config app settings

Posted written by Paul Seal on November 10, 2015 .NET Framework

This post gives you some examples of how you can check for errors when retrieving web.config app setting values.

If you have a value stored in your web config app settings, you would normally access them in the following way:

<appSettings>
   <add key="AdminEmail" value="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c38293f380c38293f38622f2321">[email&#160;protected]</a>"/>
   <add key="MinimumQuantity" value="5"/>
   <add key="ConversionRate" value="1.25"/>
</appSettings>


string adminEmail = System.Web.Configuration.WebConfigurationManager.AppSettings["AdminEmail"];

You may want to add logic around the values, but if you are doing this everywhere in your code that you need to use an app setting value, you will find a lot of duplicated code and if you don't add any logic around it, you may experience errors

Here are some helper methods for you to use, and examples of how you would call them:

using System;
using System.Web.Configuration;

namespace CodeShare.Utility
{
   public static class Helper
   {
      const string APP_SETTING_ERROR_MESSAGE = "Invalid or missing appSetting, ";

      public static string GetStringFromAppSetting(string appSettingName)
      {
         if (WebConfigurationManager.AppSettings[appSettingName] != null &amp;&amp; !String.IsNullOrEmpty(WebConfigurationManager.AppSettings[appSettingName].ToString()))
         {
           return WebConfigurationManager.AppSettings[appSettingName].ToString();
         }
         else
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
      }

      public static double GetDoubleFromAppSetting(string appSettingName)
      {
         double doubleValue = 0;
         if (WebConfigurationManager.AppSettings[appSettingName] == null || !double.TryParse(WebConfigurationManager.AppSettings[appSettingName].ToString(), out doubleValue))
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
         return doubleValue;
      }

      public static int GetIntFromAppSetting(string appSettingName)
      {
         int intValue = 0;
         if (WebConfigurationManager.AppSettings[appSettingName] == null || !int.TryParse(WebConfigurationManager.AppSettings[appSettingName].ToString(), out intValue))
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
         return intValue;
      }
   }
}

using CodeShare.Utility;

string adminEmail = GetStringFromAppSetting("AdminEmail");

double conversionRate = GetDoubleFromAppSetting("ConversionRate");

int minimumQuantity = GetIntFromAppSetting("MinimumQuantity");

Now when you run your code, if any of the app setting values are missing or invalid, it will throw an error telling you the name of the app setting that is invalid or missing.

UPDATE

I shared this post on LinkedIn and had some great input from other developers, so following their feedback, I have update the code. It is more concise, and puts the app settings into a single static class, meaning you only enter the name of the app setting once, avoiding using string literals in your code more than you need to.

using System;
using System.Web.Configuration;

namespace CodeShare.Utility
{
   public static class ConfigSettings
   {

      private const string APP_SETTING_ERROR_MESSAGE = "Invalid or missing appSetting, ";

      public static string AdminEmail { get { return GetStringFromAppSetting("AdminEmail"); } }
      public static double ConversionRate { get { return GetDoubleFromAppSetting("ConversionRate"); } }
      public static int MinimumQuantity { get { return GetIntFromAppSetting("MinimumQuantity"); } }


      public static string GetStringFromAppSetting(string appSettingName)
      {
         string setting = WebConfigurationManager.AppSettings[appSettingName] as string;
         if (String.IsNullOrEmpty(setting))
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
         return setting;
      }

      public static double GetDoubleFromAppSetting(string appSettingName)
      {
         double doubleValue = 0;
         string setting = GetStringFromAppSetting(appSettingName);
         if (!double.TryParse(setting, out doubleValue))
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
         return doubleValue;
      }

      public static int GetIntFromAppSetting(string appSettingName)
      {
         int intValue = 0;
         string setting = GetStringFromAppSetting(appSettingName);
         if (!int.TryParse(setting, out intValue))
         {
            throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
         }
         return intValue;
      }
   }
}

//Here is the new usage example
using CodeShare.Utility;

public class UsageExample
{
   private void UsingTheCode()
   {
      string adminEmail = ConfigSettings.AdminEmail;
      double conversionRate = ConfigSettings.ConversionRate;
      int minimumQuantity = ConfigSettings.MinimumQuantity;
   }
} 


Thanks Robbert and James who helped me in this post on LinkedIn

 

I hope this post was useful to some of you. I apologise if you already knew this. If you have a better way of doing it, please feel free to add it in the comments for others to see.