ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
因为一些配置属性比较多,存在多组属性,因此结合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>
原文链接http://huisky.com/blog/17011922322264
ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效的更多相关文章
- Asp.Net中使用Couchbase——Memcached缓存使用篇
Asp.Net中使用Couchbase——Memcached缓存使用篇 前言 在上一篇Asp.Net中使用Couchbase——Memcached缓存入门篇http://www.cnblogs.com ...
- 【ASP.NET 系列】浅谈缓存技术在ASP.NET中的运用
本篇文章虽不谈架构,但是Cache又是架构中不可或缺的部分,因此,在讲解Cache的同时,将会提及到部分架构知识,关于架构部分,读者可以不用理解,或者直接跳过涉及架构部分的内容 你只需关心Cache即 ...
- ASP.NET下MVC设计模式的实现
[转载]MVC架构在Asp.net中的应用和实现 转载自:http://www.cnblogs.com/baiye7223725/archive/2007/06/07/775390.aspx 摘要:本 ...
- XML反序列化遇到数字型节点值为空导致反序列化异常
实体类: [XmlRoot("stream")] public class _30320DuisiFukuanQueryResponseModel : ResponseModelB ...
- 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署 ClassCastException异常 反射 ...
- 初探Net框架下的XML编程技术
一.前言: XML是微软.Net战略的一个重要组成部分,而且它可谓是XML Web服务的基石,所以掌握.Net框架下的XML技术自然显得非常重要了.本文将指导大家如何运用C#语言完成.Net框架下的X ...
- 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程
反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) 背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...
- ASP.NET Core中的Http缓存
ASP.NET Core中的Http缓存 Http响应缓存可减少客户端或代理对web服务器发出的请求数.响应缓存还减少了web服务器生成响应所需的工作量.响应缓存由Http请求中的header控制. ...
- Xml反序列化记录
1.概述 公司项目遇到一个需要对接webservice的,webservice大部分用的都是xml来传输的,这里记录一下xml反序列化遇到的问题 2.xml工具类 xml序列化: public sta ...
随机推荐
- ural1523 K-inversions
K-inversions Time limit: 1.0 secondMemory limit: 64 MB Consider a permutation a1, a2, …, an (all ai ...
- Angular2 - Starter - Routes, Route Resolver
在基于Angualr的SPA中,路由是一个很重要的部分,它帮助我们更方便的实现页面上区域的内容的动态加载,不同tab的切换,同时及时更新浏览器地址栏的URN,使浏览器回退和前进能导航到历史访问的页面. ...
- mac ox 配置java和maven
参考http://www.cnblogs.com/iOS-mt/p/5726380.html 以及http://blog.csdn.net/done58/article/details/5113805 ...
- ZBUS = MQ + RPC
http://git.oschina.net/rushmore/zbus http://my.oschina.net/sbz/blog Readme.md 18.02 KB ZBUS = MQ + ...
- jsp显示计算数值, 四舍五入
<script>document.write(Math.round(<%= rs_MFM.getInt("PVRCompl") %>/<%= rs_M ...
- iOS常用宏定义
转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...
- android测试之——mokeyrunner上(二)
以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...
- iOS开发——判断邮箱格式
//判断邮箱格式 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@ ...
- Qt下libusb-win32的使用(转)
源:Qt下libusb-win32的使用(一)打印设备描述符 主要是在前一篇的基础上,学习libusb-win32的API使用.程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的 ...
- BZOJ 2705 [SDOI2012]Longge的问题 ——Dirichlet积
[题目分析] 狄利克雷卷积. 然后直接求出欧拉函数,计算和即可. [代码] #include <cstdio> #include <cstring> #include < ...