本部落格程式使用過程中,查看錯誤記錄檔,發現以下錯誤:
<ErrorItem>
<Date>2007-12-13 18:07:26 出錯檔案:http://xianfen.net/Default.aspx</Date>
<Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。 內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
<Date>2007-12-13 18:07:27 出錯檔案:http://xianfen.net/Default.aspx</Date>
<Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。 內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
<Date>2007-12-13 21:01:47 出錯檔案:http://www.xianfen.net/Category5_1.aspx?ClassID=5&page=1</Date>
<Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。 內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
<Date>2007-12-14 9:21:02 出錯檔案:http://www.xianfen.net/Archive200712.aspx?Year=2007&Month=12</Date>
<Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。 內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
...
很多“未將對象引用設定到對象的執行個體。”,仔細審查每一行程式,沒發現使用Null 物件的情況。無奈之時,在本機上隨便調試,發現異常程式碼片段之一為:
public static string BlogTitle
{
get
{
if (HttpContext.Current.Cache["BlogTitle"] == null)
{
HttpContext.Current.Cache["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
}
return HttpContext.Current.Cache["BlogTitle"].ToString(); //在此拋出異常"未將對象引用設定到對象執行個體"
}
}
為了提高效能,本部落格系統多處使用緩衝,但每次取快取資料時都檢查是否為空白啊?只好 Google,發現有和我遇到一樣問題的,原來緩衝 Cache 在記憶體不足時會移除,看來虛擬空間的記憶體緊張到極點了,剛設定的緩衝就被移除了!
將Cache用Application代替:
public static string BlogTitle
{
get
{
if (HttpContext.Current.Application["BlogTitle"] == null)
{
HttpContext.Current.Application["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
}
return HttpContext.Current.Application["BlogTitle"].ToString();
}
}
問題解決!