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

所属分类: 网络编程 / ASP.NET 阅读数: 613
收藏 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图片上传实现预览效果的简单代码

这篇文章介绍了Asp.net图片上传实现预览效果的简单代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET动态设置页面标题的方法详解

这篇文章介绍了ASP.NET动态设置页面标题的方法详解,有需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET中获取URL重写前的原始地址详解

在ASP.NET中,如果你使用了URL重写,通过HttpContext.Request获取到的是重写后的地址。如果这个地址要返回给客户端(比如Redirect),我们一般希望是重写前的友好地址。
收藏 0 赞 0 分享

.Net实现合并文件的具体方法

这篇文章介绍了.Net实现合并文件的具体方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

asp.net 初始化文本框的小例子

这篇文章介绍了asp.net 初始化文本框的小例子,有需要的朋友可以参考一下,希望对你有所帮助
收藏 0 赞 0 分享

用WPF实现屏幕文字提示的实现方法

本文介绍WPF应用程序实现在屏幕上显示一行或多行文字通知。它没有标题栏和最大化最小化等按钮,可以有半透明背景以使文字的显示更清晰,鼠标点击后提示消失。
收藏 0 赞 0 分享

.NET更新Xml中CDATA内容的方法实例

这篇文章介绍了.NET更新Xml中CDATA内容的方法实例,有需要的朋友可以参考一下
收藏 0 赞 0 分享

.NET中弹出对话框的方法汇总

下面是本人对常用对话框使用的汇总,希望对大家有所帮助,同时也欢迎大家补充。
收藏 0 赞 0 分享

ASP.NET动态生成静态页面的实例代码

生成静态页有很多好处,可以缓解服务器压力、方便搜索网站搜索等等,下面介绍一下生成静态页的实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

利用.net控件实现下拉导航菜单制作的具体方法

这篇文章介绍了利用.net控件实现下拉导航菜单制作的具体方法,有需要的朋友可以参考一下,希望对你有所帮助
收藏 0 赞 0 分享
查看更多