Load Any Static Configurable Data Using JSON File Instead Of Web Config File

Load any static configurable data using JSON file instead of web config file.
 
Basically if we want to read any static data from web config file than we need to write a line of code for each data like folowing.
  1. ConfigurationManager.Appsettings[“CountryCode”].ToString;  
  2. ConfigurationManager.Appsettings[“StateCode”].ToString;  
  3. ConfigurationManager.Appsettings[“DefaultValue”].ToString;  
  4. ConfigurationManager.Appsettings[“ApiKey”].ToString;  
  5. ConfigurationManager.Appsettings[“ServiceURL1”].ToString;  
  6. ConfigurationManager.Appsettings[“ServiceURL2”].ToString;  
  7. ConfigurationManager.Appsettings[“ServiceURL3”].ToString;  
Q. Why we need to create the JSON file?
 
Answer
  1. Easy and clean to maintain configurable data.
  2. No need to hit web config file for read each configuration data.
  3. Easy to modify and change configurable data on live also.
Steps to create and use Json file.
  1. we need to create a json file and initialize each values accordingly .
  2. Create classes for initialize json data .
  3. Write a singleton class for load json file once and use globally in the project.
Step 1
 
Json file name configuration.json
  1. {  
  2.     "CommonConfigurationData": {  
  3.         "DefaultCountryCode""IN",  
  4.         " DefaultStateCode""BR",  
  5.         "DefaultValue""12345"  
  6.     },  
  7.     "ApiCredentails": {  
  8.         "ApiKey""ABC……………",  
  9.         "ServiceURL1"" https://localhost:44360/"  
  10.         "ServiceURL2"" https://localhost:44361/"  
  11.         "ServiceURL3"" https://localhost:44362/"  
  12.     }  
  13. }  
Step 2
  1. public class CommonConfigurationData {  
  2.     public string DefaultCountryCode {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string DefaultStateCode {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string DefaultValue {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  
  15. public class ApiCredentails {  
  16.     public string ApiKey {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public string ServiceURL1 {  
  21.         get;  
  22.         set;  
  23.     }  
  24.     public string ServiceURL2 {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     public string ServiceURL3 {  
  29.         get;  
  30.         set;  
  31.     }  
  32. }  
  33. public class LocalConfigurationdata {  
  34.     public CommonConfigurationData CommonConfigurationData {  
  35.         get;  
  36.         set;  
  37.     }  
  38.     public ApiCredentails ApiCredentails {  
  39.         get;  
  40.         set;  
  41.     }  
  42. }  
Step 3
 
Add json file path in web config file.
  1. <add key="ApplicationConfigPath" value="~/ApplicationConfig.json" />  
Step 4
 
Write a singleton class for load configuration file
  1. public sealed class Configuration {  
  2.     private static Configuration instance = null;  
  3.     private static readonly object padlock = new object();  
  4.     Configuration() {}  
  5.     public static Configuration Instance {  
  6.         get {  
  7.             bool acquiredLock = false;  
  8.             try {  
  9.                 Monitor.Enter(padlock, ref acquiredLock);  
  10.                 if (instance == null) {  
  11.                     instance = new Configuration {  
  12.                         LocalConfiguration = JsonConvert.DeserializeObject < LocalConfiguration > (File.ReadAllText(ConfigurationManager.AppSettings["ApplicationConfigPath"].ToString()))  
  13.                     };  
  14.                 }  
  15.             } catch (Exception ex) {  
  16.                 ex.ToString();  
  17.                 // Common.Loggerx.Error(ex, ex.StackTrace);  
  18.             } finally {  
  19.                 if (acquiredLock) {  
  20.                     Monitor.Exit(padlock);  
  21.                 }  
  22.             }  
  23.             return instance;  
  24.         }  
  25.     }  
  26.     public LocalConfigurationData LocalConfiguration {  
  27.         get;  
  28.         set;  
  29.     }  
  30. }  
Step 5
 
Use this configuration data globally.
 
String defaultCountryCode= Configuration.Instance.LocalConfiguration.
 
CommonConfigurationData.DefaultCountryCode
 
Once we write this code than for next time if we want to add or modify any new configurationdata no need to put any extra effort, only need to add properties in the class and same in json file.
Next Recommended Reading CustomError mode in web.config file