无为清净楼资源网 Design By www.qnjia.com
因为一些配置属性比较多,存在多组属性,因此结合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>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
标签:
ASP.NET,xml,置文件
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 赞多《序章》[FLAC/分轨][129.13MB]
- 群星《完美的她 影视原声带》[320K/MP3][51.98MB]
- 群星《完美的她 影视原声带》[FLAC/分轨][141.05MB]
- 山风点伙.1997-深藏不露【立得】【WAV+CUE】
- 李幸倪.2018-First.Of.All.Live.Concert.3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 群星《你就在我身边 电影原声专辑》[320K/MP3][26.68MB]
- 群星《你就在我身边 电影原声专辑》[FLAC/分轨][70.27MB]
- 班得瑞原装进口《20周年精选集》1CD[APE/CUE分轨][303MB]
- 群星.2009-NOON.D.广东.2CD【新世纪】【WAV+CUE】
- 洪卓立.2012-GROWN.UP【英皇娱乐】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 金佩姗.1997-喜新恋旧【现代派】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】