本博客程序使用过程中,查看错误日志,发现以下错误:
很多“未将对象引用设置到对象的实例。”,仔细审查每一行程序,没发现使用空对象的情况。无奈之时,在本机上随便调试,发现异常代码段之一为:
为了提高性能,本博客系统多处使用缓存,但每次取缓存数据时都检查是否为空啊?只好 Google,发现有和我遇到一样问题的,原来缓存 Cache 在内存不足时会移除,看来虚拟空间的内存紧张到极点了,刚设置的缓存就被移除了!
将Cache用Application代替:
问题解决!
<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>
...<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>
很多“未将对象引用设置到对象的实例。”,仔细审查每一行程序,没发现使用空对象的情况。无奈之时,在本机上随便调试,发现异常代码段之一为:
public static string BlogTitle
{
get
{
if (HttpContext.Current.Cache["BlogTitle"] == null)
{
HttpContext.Current.Cache["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
}
return HttpContext.Current.Cache["BlogTitle"].ToString(); //在此抛出异常"未将对象引用设置到对象实例"
}
}
{
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();
}
}
{
get
{
if (HttpContext.Current.Application["BlogTitle"] == null)
{
HttpContext.Current.Application["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
}
return HttpContext.Current.Application["BlogTitle"].ToString();
}
}
问题解决!