Geeks With Blogs

Pankaj Sharma Staying Connected In A Disconnected World...

The title and scope of the article may force you to argue as web.config files are already there to fulfill the purpose of keeping application specific configuration if any. But i encountered a case where the application's configuration settings in appSettings section grew larger and larger. Providing a flexibility to customer to change any setting value on his own turned out to be problem as it cluttered the web.config file. File eventually became large enough that a setting key value happened to miss the user's eye.


I tried to find out all the possibilities of removing app settings from web.config to another configuration file. I found out an easy approach where in a new .xml file was created and a piece of custom code that loaded file and assigned the values to corresponding properties of a custom class. Let talk the real business of how i did it.

A new configuration file AppConfigurations.xml was created in App_Data folder of the application. Format the file as shown below -

ViewAppconfig

A new code file was created under Configuration folder with the name AppConfigurations.cs. This file is a wrapper class of the xml file. It contains as many public read only variables as tags in xml file. Each class member corresponds the appConfig tag in xml. You can see all the read only members have been assigned values in the constructor of the class.

       1:  public static class AppConfigurations 
       2:      { 
       3:          public static readonly string AppBasePath; 
       4:          public static readonly string AuthenticationKey; 
       5:          public static read only string ErrorLogFilePath;       
       6:          static AppConfigurations() 
       7:          { 
       8:              DataSet file = new DataSet(); 
       9:              string path = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\AppConfigurations.xml"; 
      10:              file.ReadXml(path); 
      11:              if (file.Tables.Count > 0) 
      12:              { 
      13:                  DataTable cFile = file.Tables[0]; 
      14:                  cFile.PrimaryKey = new DataColumn[] { cFile.Columns[0] }; 
      15:                  AppBasePath = cFile.Rows.Find("BasePath")["value"].ToString(); 
      16:                  AuthenticationKey = cFile.Rows.Find("AuthenticationKey")["value"].ToString(); 
      17:                  ErrorLogFilePath = cFile.Rows.Find("ErrorLogFilePath")["value"].ToString(); 
      18:              } 
      19:          }        
      20:      } 
The read only fields allows to be assigned values once only in the initializer or in the constructor of the class. I choose the static class as app configuration settings are going to be same for the entire application hence does not need any copy.

You can test if the settings are properly read or not by showing them on a page.

ViewAppConfigUI

Here is the code that displays the text on the Default.aspx page.

   1:  public partial class _Default : System.Web.UI.Page 
   2:      { 
   3:          protected void Page_Load(object sender, EventArgs e) 
   4:          { 
   5:              System.Text.StringBuilder sb = new System.Text.StringBuilder(100); 
   6:              sb.Append("<p>AppBasePath :" + Configuration.AppConfigurations.AppBasePath + "</p>"); 
   7:              sb.Append("<p>AuthenticationKey :" + Configuration.AppConfigurations.AuthenticationKey + "</p>"); 
   8:              sb.Append("<p>ErrorLogFilePath :" + Configuration.AppConfigurations.ErrorLogFilePath + "</p>"); 
   9:              ltContent.Text = sb.ToString(); 
  10:          } 
  11:      } 

But this implementation is reasonably different from the one using web.config file. As soon as any change is done in web.config file appdomain is restarted and new fresh value of app settings are available in the application.

Lets emulate this behaviour by using filesystemwatcher object and binding the changed event to a handler. As soon as any change is done in the AppConfigurations.xml file file’s changed event is raised and in the event handler we unload the application domain. It forces to restart the app domain on the subsequent requests.

   1:  public static class AppConfigurations 
   2:      { 
   3:          public static readonly string AppBasePath; 
   4:          public static readonly string AuthenticationKey; 
   5:          public static readonly string ErrorLogFilePath; 
   6:          static System.IO.FileSystemWatcher fsw; 
   7:          static AppConfigurations() 
   8:          { 
   9:              DataSet file = new DataSet(); 
  10:              …… 
  11:              SetupFileWatcher(AppDomain.CurrentDomain.BaseDirectory + "App_Data"); 
  12:          } 
  13:          static void SetupFileWatcher(string dirPath) 
  14:          { 
  15:              fsw = new System.IO.FileSystemWatcher(dirPath); 
  16:              fsw.IncludeSubdirectories = false; 
  17:              fsw.Filter = "*.xml"; 
  18:              fsw.NotifyFilter = System.IO.NotifyFilters.LastWrite; 
  19:              fsw.Changed += new System.IO.FileSystemEventHandler(fsw_Changed); 
  20:              fsw.EnableRaisingEvents = true; 
  21:          } 
  22:          static void fsw_Changed(object sender, System.IO.FileSystemEventArgs e) 
  23:          { 
  24:              try 
  25:              { 
  26:                  AppDomain.Unload(AppDomain.CurrentDomain); 
  27:              } 
  28:              catch 
  29:              { 
  30:                  //Log the exception 
  31:              } 
  32:          } 
  33:      }

Happy coding!!

 

Posted on Wednesday, April 25, 2012 8:28 PM | Back to top


Comments on this post: Create and Use a Custom Application Configuration File

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © pankajsharma | Powered by: GeeksWithBlogs.net