Under ASP. NET xml deserialization and cache dependency are used to realize the real time effect of personalized configuration files

  • 2021-09-12 00:47:54
  • OfStack

Because there are many configuration attributes and there are many groups of attributes, the configuration culture is automatically resolved and stored in the cache by combining xml parsing and caching technology, and the cache depends on updating the configuration content in real time.

The core method of deserializing configuration files and storing them in cache:


public Class.Settings GetSettings()
 {
 if (HttpRuntime.Cache["settings"] != null)
  return (Class.Settings)HttpRuntime.Cache["settings"];
 string rootPath = GetPath();
 #region rootPath
 if (rootPath == "")
 {
  log.Write(MsgType.Fatal, " Configuration file root directory rootPath Empty ");
  return null;
 }
 else
 {
  if (!rootPath.EndsWith("\\"))
  rootPath += "\\";
  rootPath = rootPath + "settings\\settings.config";
 }
 #endregion
 if (!File.Exists(rootPath))
 {
  log.Write(MsgType.Fatal, " Configuration file root directory rootPath Empty ");
  return null;
 }
 string content = File.ReadAllText(rootPath, Encoding.Default);
 Class.Settings model = PublicMethod.XmlSerialize.DeserializeXML<Class.Settings>(content);
 log.Write(MsgType.Information, " Read configuration file ");
 CacheDependency cd = new CacheDependency(rootPath);
 HttpRuntime.Cache.Add("settings", model, cd, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.High, null);
 return model;
 }

The above method for automatically obtaining rootPath:


 /// <summary>
 ///  The method of taking the current root directory  
 /// </summary>
 private static string GetPath()
 {
 string rootPath = "";
 System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
 //WebDev.WebServer visual studio web server
 //xxx.vhost  Winform
 //w3wp   IIS7
 //aspnet_wp  IIS6
 //iisexpress  vs2013
 string processName = p.ProcessName.ToLower();
 if (processName == "aspnet_wp" || processName == "w3wp" || processName == "webdev.webserver" || processName == "iisexpress")
 {
  if (System.Web.HttpContext.Current != null)
  rootPath = System.Web.HttpContext.Current.Server.MapPath("~/");
  else // Null when the control is used in the trigger of the timer 
  {
  rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
  }
 }
 return rootPath;
 }

For the definition of Settings entity class, it should be noted that the entity class here should correspond to the settings configuration file, otherwise deserialization will go wrong:


[XmlRoot(Namespace = "", IsNullable = false, ElementName = "settings")]
public class Settings
{
 #region  Attribute 
 [XmlElement("logger")]
 public LoggerConfig logger { get; set; }
 #endregion
 #region  Subclass 
 [XmlType(TypeName = "logger")]
 public class LoggerConfig
 {
 public string loglevel { get; set; }
 public string savepath { get; set; }
 } 
 #endregion
}

settings. Content instance of config


<?xml version='1.0' encoding='utf-8'?>
 <settings>
 <logger>
 <loglevel>0</loglevel>
 <savepath>d:\log</savepath>
 </logger>
<queryurl>http://11.56.254.234:88/shashachaxunserver/shashachaxun</queryurl>
<receiveurl>http://172.16.1.131:88/ThirdPay/ChinaUMS/xml.aspx</receiveurl>
<turnurl>http://172.16.1.131:88/ThirdPay/ChinaUMS/query.aspx</turnurl>
 </chinaums>
 </settings>

Related articles: