ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
2017-01-20 09:11
981 查看
因为一些配置属性比较多,存在多组属性,因此结合xml解析、缓存技术,实现配置文化的自动解析、存入缓存、缓存依赖实时更新配置内容。
配置文件反序列化存入缓存的核心方法:
上面自动获取rootPath的方法:
Settings实体类的定义,要注意,这里的实体类要和settings配置文件对应,否则反序列化会出错:
settings.config的内容实例
原文链接http://huisky.com/blog/17011922322264
配置文件反序列化存入缓存的核心方法:
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>
原文链接http://huisky.com/blog/17011922322264
相关文章推荐
- ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效
- 利用XML序列化和Asp.Net Web缓存实现站点配置文件
- 用XML反序列化快速完成ASP.NET配置文件
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- ASP.NET 2.0 中一种新的方法实现对配置文件的加密
- ASP.NET 使用ConfigurationManager读写配置文件
- 使用.NET读取XML文件,.net开发,.net,ASP.NET
- 技巧和诀窍:使用ASP.NET 2.0 输出缓存替换的功能实现“甜圈缓存(Donut Caching)”
- 使用asp.net 实现在ie中打开autocad格式文件
- ASP.NET:使用web.config文件进行配置
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- 使用ASP.NET读取XML文件
- 使用XML读写删除功能来实现资源文件配置
- ASP.NET使用Response.WriteFile(filename);实现 文件下载
- ASP.NET 缓存(五)--使用文件依赖项缓存页输出http://www.opent.cn/?action=show&id=192
- 技巧和诀窍:使用ASP.NET 2.0 输出缓存替换的功能实现“甜圈缓存(Donut Caching)”
- 在Asp.Net里使用动态缓存文件
- 关于使用ASP实现文件缓存的解决方案
- 转发一个Asp.NET 操作配置文件的使用心得
- asp.net 2.0 使用sqlserver2005 新的通知数据缓存实现