ASP.NET下使用xml反序列化、緩存依賴(lài)實(shí)現(xiàn)個(gè)性化配置文件的實(shí)時(shí)生效
因?yàn)橐恍┡渲脤傩员容^多,存在多組屬性,因此結(jié)合x(chóng)ml解析、緩存技術(shù),實(shí)現(xiàn)配置文化的自動(dòng)解析、存入緩存、緩存依賴(lài)實(shí)時(shí)更新配置內(nèi)容。
配置文件反序列化存入緩存的核心方法:
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;
}
上面自動(dòng)獲取rootPath的方法:
/// <summary>
/// 取當(dāng)前根目錄的方法
/// </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 //當(dāng)控件在定時(shí)器的觸發(fā)程序中使用時(shí)就為空
{
rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
}
}
return rootPath;
}
Settings實(shí)體類(lèi)的定義,要注意,這里的實(shí)體類(lèi)要和settings配置文件對(duì)應(yīng),否則反序列化會(huì)出錯(cuò):
[XmlRoot(Namespace = "", IsNullable = false, ElementName = "settings")]
public class Settings
{
#region 屬性
[XmlElement("logger")]
public LoggerConfig logger { get; set; }
#endregion
#region 子類(lèi)
[XmlType(TypeName = "logger")]
public class LoggerConfig
{
public string loglevel { get; set; }
public string savepath { get; set; }
}
#endregion
}
settings.config的內(nèi)容實(shí)例
<?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>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
輕量級(jí)ORM框架Dapper應(yīng)用之實(shí)現(xiàn)DTO
本文詳細(xì)講解了使用Dapper實(shí)現(xiàn)DTO的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
.NET性能調(diào)優(yōu)之一:ANTS Performance Profiler的使用介紹
本系列文章主要會(huì)介紹一些.NET性能調(diào)優(yōu)的工具、Web性能優(yōu)化的規(guī)則(如YSlow)及方法等等內(nèi)容。成文前最不希望看到的就是園子里不間斷的“哪個(gè)語(yǔ)言好,哪個(gè)語(yǔ)言性能高”的爭(zhēng)論,不多說(shuō),真正的明白人都應(yīng)該知道這樣的爭(zhēng)論有沒(méi)有意義,希望我們能從實(shí)際性能優(yōu)化的角度去討論問(wèn)題2013-01-01
.NET實(shí)現(xiàn)WebSocket服務(wù)端即時(shí)通信實(shí)例
本篇文章主要介紹了.NET實(shí)現(xiàn)即時(shí)通信,WebSocket服務(wù)端實(shí)例 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
ASP.NET中GridView 重復(fù)表格列合并的實(shí)現(xiàn)方法
本文通過(guò)GridView 和 Repeater 解決有關(guān)表格顯示數(shù)據(jù)重復(fù)的數(shù)據(jù)列和并的方法,非常實(shí)用,感興趣的朋友一起看下吧2016-08-08
.NET?Core控制臺(tái)應(yīng)用ConsoleApp讀取appsettings.json配置文件
這篇文章介紹了.NET?Core控制臺(tái)應(yīng)用ConsoleApp讀取appsettings.json配置文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
詳解.Net Core 權(quán)限驗(yàn)證與授權(quán)(AuthorizeFilter、ActionFilterAttribute)
這篇文章主要介紹了.Net Core 權(quán)限驗(yàn)證與授權(quán)(AuthorizeFilter、ActionFilterAttribute),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

