• 关于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种情况我觉得都是存在的,每次都只是解决了其中一个问题。

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

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

  • 相关阅读:
    SQL Server -- 数据收缩详解
    查看SQL数据库表大小
    drop、truncate和delete的区别
    【汇总】Windows linux 敏感目录 路径汇总——主要是主机配置文件、web配置文件
    BFS_拓扑排序 使用图遍历思想也是OK的 虽然代码多了点
    深度森林原理及实现——原来是借鉴了残差网络和highway的思想,将其用于树类算法
    BFS——单词接龙,这种题一定要当心环路
    BFS——克隆图,发现直接copy会出现某些环路的边会丢失,还是要先copy节点,再copy边
    双指针——最接近的三数之和,细节处理还是很关键的
    双指针——三角形计数,就是一些细节处理要严谨,否则还是容易出错
  • 原文地址:https://www.cnblogs.com/bigbigworld/p/3715817.html
Copyright © 2020-2023  润新知