In ASP. NET, xml deserialization and cache dependency are used to bring personalized configuration files into effect in real time. xml serialization
Because many configuration attributes exist and multiple groups of attributes, the configuration culture can be automatically parsed, stored in the cache, And the configuration content can be updated in real time based on the xml parsing and cache technologies.
Core Method for storing configuration file deserialization into 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, "the configuration file root directory rootPath is empty"); return null;} else {if (! RootPath. EndsWith ("\") rootPath + = "\"; rootPath = rootPath + "settings \ settings. config" ;}# endregion if (! File. exists (rootPath) {log. write (MsgType. fatal, "the configuration File root directory rootPath is 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 automatically obtains the rootPath:
/// <Summary> /// Method for retrieving 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" | proce SsName = "webdev. webserver" | processName = "iisexpress") {if (System. Web. HttpContext. Current! = Null) rootPath = System. Web. HttpContext. Current. Server. MapPath ("~ /"); Else // when the control is used in the timer trigger program, it is null {rootPath = System. AppDomain. CurrentDomain. BaseDirectory ;}} return rootPath ;}
Settings object class definition, note that the object class here must correspond to the settings configuration file, otherwise deserialization will fail:
[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}
Content instance of settings. 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>
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!