国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET下使用xml反序列化、緩存依賴實現個性化配置文件的實時生效

2024-07-10 13:31:58
字體:
來源:轉載
供稿:網友

因為一些配置屬性比較多,存在多組屬性,因此結合xml解析、緩存技術,實現配置文化的自動解析、存入緩存、緩存依賴實時更新配置內容。

配置文件反序列化存入緩存的核心方法:

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, "配置文件根目錄rootPath為空");  return null; } else {  if (!rootPath.EndsWith("//"))  rootPath += "//";  rootPath = rootPath + "settings//settings.config"; } #endregion if (!File.Exists(rootPath)) {  log.Write(MsgType.Fatal, "配置文件根目錄rootPath為空");  return null; } string content = File.ReadAllText(rootPath, Encoding.Default); Class.Settings model = PublicMethod.XmlSerialize.DeserializeXML<Class.Settings>(content); log.Write(MsgType.Information, "讀取配置文件"); CacheDependency cd = new CacheDependency(rootPath); HttpRuntime.Cache.Add("settings", model, cd, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.High, null); return model; }

上面自動獲取rootPath的方法:

 /// <summary> /// 取當前根目錄的方法  /// </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 //當控件在定時器的觸發程序中使用時就為空  {  rootPath = System.AppDomain.CurrentDomain.BaseDirectory;  } } return rootPath; }

Settings實體類的定義,要注意,這里的實體類要和settings配置文件對應,否則反序列化會出錯:

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

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>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南漳县| 云安县| 确山县| 尼勒克县| 宁武县| 商水县| 东明县| 贵溪市| 敦化市| 包头市| 香河县| 遂昌县| 富裕县| 岗巴县| 淮阳县| 吴堡县| 郸城县| 双桥区| 社会| 泰顺县| 乐至县| 若尔盖县| 深水埗区| 齐齐哈尔市| 霸州市| 穆棱市| 广德县| 文登市| 宁乡县| 罗山县| 乌拉特中旗| 徐闻县| 长春市| 治多县| 余庆县| 淳化县| 无极县| 辛集市| 宁夏| 伊川县| 盐边县|