這篇文章主要為大家展示了“ASP.NET中如何使用xml反序列化、緩存依賴實現(xiàn)個性化配置文件”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASP.NET中如何使用xml反序列化、緩存依賴實現(xiàn)個性化配置文件”這篇文章吧。

配置文件反序列化存入緩存的核心方法:
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>
/// 取當(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)控件在定時器的觸發(fā)程序中使用時就為空
{
rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
}
}
return rootPath;
}Settings實體類的定義,要注意,這里的實體類要和settings配置文件對應(yīng),否則反序列化會出錯:
[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的內(nèi)容實例
<?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中如何使用xml反序列化、緩存依賴實現(xiàn)個性化配置文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文題目:ASP.NET中如何使用xml反序列化、緩存依賴實現(xiàn)個性化配置文件-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://chinadenli.net/article34/pshpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作、網(wǎng)站制作、建站公司、微信小程序、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容