ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效

所属分类: 网络编程 / ASP.NET 阅读数: 654
收藏 0 赞 0 分享

因为一些配置属性比较多,存在多组属性,因此结合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 JavaScript插件  JavaScript Function Outliner

一个JavaScript Function Outliner插件 第四版本 支持内嵌javascript,且可以对javascript进行压缩
收藏 0 赞 0 分享

asp.net汉字转拼音和获取汉字首字母的代码

在网上找到的好东西。以后asp.net下汉字转成拼音就方便多了
收藏 0 赞 0 分享

asp.net 多字段模糊查询代码

经常用到多字段的模糊查询,上面的函数可以实现,例如strKeyWords值为“脚本之家”时
收藏 0 赞 0 分享

OpenCms 带分页的新闻列表

有一些网友在新闻列表分页上还遇到一些问题,正好这个blog上也忘记了此部分内容,现在补充上,功能是实现了,可以自己再做些优化,OpenCms7.0.5下测试通过,内容如下(编辑器的插入代码功能有问题,就直接把代码粘上了
收藏 0 赞 0 分享

URLRewriter最简单入门介绍 URLRewriter相关资源

配置好后,查看日志看到的状态都是200,IIS直接认为这个文件是存在的了, 而不是301,或302,这在某些情况下可能会不适用,比如:搜索引擎优化时目录或文件调整。
收藏 0 赞 0 分享

asp.net Repeater取得CheckBox选中的某行某个值

Repeater取得CheckBox选中的某行某个值的实现代码
收藏 0 赞 0 分享

asp.net清空Cookie的两种方法

清空cookie
收藏 0 赞 0 分享

asp.net一些很酷很实用的.Net技巧第1/2页

方便使用asp.net编程的朋友,都是一些非常有用的东西
收藏 0 赞 0 分享

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结
收藏 0 赞 0 分享

使用UserControl做网站导航条的思路 分析

使用UserControl做网站导航条的思路 分析
收藏 0 赞 0 分享
查看更多