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 ...
随机推荐
- svn版本库通过svn://127.0.0.1/不能导出的问题解决了!!
svn:本地file:///E:/SvnServerHome没问题,但是换为svn://192.168.1.100/SvnServerHome 报异常. 配置http协议访问svn 原文:http:/ ...
- CodeForces 616A Comparing Two Long Integers
水题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; +; ...
- dependency injection(2)
https://segmentfault.com/a/1190000002424023
- LNMP(Linux+Nginx+Mysql+PHP---源码)环境搭建
LNMP(Linux+Nginx+Mysql+PHP(Perl)) Linux:[root@dep5 mysql]# cat /etc/issueRed Hat Enterprise Linux Se ...
- ASP.NET AJAX 创建类
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...
- memcached 第二篇----安装使用
摘要:set add replace get delete gets cas stats 和 flush_all 命令 获取所有key .你可以使用MemCachedClient的statsItem ...
- c#中反射
在.Net 中,程序集(Assembly)中保存了元数据(MetaData)信息,因此就可以通过分析元数据来获取程序集中的内容,比如类,方法,属性等,这大大方便了在运行时去动态创建实例. MSDN解释 ...
- 数据可视化-OmniGraffle软件
OmniGraffle Pro for mac破解版是一款运行在MAC OS平台上的思维导图流程图制作软件,通过思维导图软件(OmniGraffle Pro MAC)帮你组织头脑中思考的信息,组织头脑 ...
- ZOJ 1108 & HDU 1160 - FatMouse's Speed
题目大意:给你n只老鼠的体重w和速度s,让你找出最长的子序列使得w[i] < w[j] 且 s[i] > s[j] (i < j).求最长序列的长度并输出该序列. LIS(Longe ...
- 拓扑排序(Topological)
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<stack&g ...