关于URLRewriter报错:System.NullReferenceException: 未将对象引用设置到对象的实例 的解决

检查网站日期,发现内容如下:
System.NullReferenceException: 未将对象引用设置到对象的实例。 在 URLRewriter.ModuleRewriter.Rewrite(String requestedPath, HttpApplication app) 在 URLRewriter.BaseModuleRewriter.BaseModuleRewriter_AuthorizeRequest(Object sender, EventArgs e) 在 System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

URLRewriter是一个开源的Url重写的东东,当然现在可能用的人少了。
网上找到一篇类似的:http://www.cnblogs.com/notus/archive/2007/07/04/710493.html,不过貌似他的问题是在应用层去解决的。
所以还是看了下URLRewriter的源码。

修改如下,上代码,不说话。
using System;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Configuration;
using System.Xml.Serialization;

namespace URLRewriter.Config
{
	[Serializable()]
	[XmlRoot("RewriterConfig")]
	public class RewriterConfiguration
	{
        private static volatile RewriterConfiguration _singleCache = null;
        private static object _syncLock=new object();

	    private RewriterConfiguration()
	    {

	    }

	    // private member variables
		private RewriterRuleCollection rules;			// an instance of the RewriterRuleCollection class...

		/// <summary>
		/// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
		/// the Web.config file.  It uses XML deserialization to convert the XML structure in Web.config into
		/// a <b>RewriterConfiguration</b> instance.
		/// </summary>
		/// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
		public static RewriterConfiguration GetConfig()
		{
            //old
            //if (HttpContext.Current.Cache["RewriterConfig"] == null)
            //    HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));

            //return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];

            //第一次改动
            //var tmp=HttpContext.Current.Cache["RewriterConfig"];
            //if (tmp == null)
            //{
            //    HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));
            //    tmp = (RewriterConfiguration)HttpContext.Current.Cache["RewriterConfig"];
            //}
            //return (RewriterConfiguration)tmp;

            //第二次改动
            //if (HttpContext.Current.Cache["RewriterConfig"] == null)
            //    HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"),null,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration);

            //return (RewriterConfiguration)HttpContext.Current.Cache["RewriterConfig"];

            //tims 3
            if (_singleCache == null)
            {
                Monitor.Enter(_syncLock);
                if (_singleCache == null)
                {
                    _singleCache = (RewriterConfiguration)ConfigurationSettings.GetConfig("RewriterConfig");   
                }
                Monitor.Exit(_syncLock);
            }
            return _singleCache;

		}

		#region Public Properties
		/// <summary>
		/// A <see cref="RewriterRuleCollection"/> instance that provides access to a set of <see cref="RewriterRule"/>s.
		/// </summary>
		public RewriterRuleCollection Rules
		{
			get
			{
				return rules;
			}
			set
			{
				rules = value;
			}
		}
		#endregion
	}
}

  前面2次还是有问题,第三次才ok.

第一次修改时考虑到缓存被GC给回收造成的,因为实际上报这个错误大多是在大并发的情况下。

第二次修改是考虑到缓存自动失效造成的。

第三次是用单例去避开了缓存失效的问题。

上面2种情况我觉得都是存在的,每次都只是解决了其中一个问题。

第三次实际没有去正面解决如何有效控制缓存失效造成类似的问题,情况急,先记下备忘。稍后再考虑和补齐这个问题。

有类似经验的也希望不吝赐教.

posted on 2014-05-08 11:44  Tim Lee  阅读(370)  评论(0编辑  收藏  举报

导航