HttpRuntime CacheInternal NULL reference exception while reading user sessions (Reflection)

前端 未结 3 1077
执笔经年
执笔经年 2021-01-19 16:11

After some updates on our windows servers(2008R2 ,2012) Asp.net application throwing error:

var obj_1 = typeof(HttpRuntime).GetProperty(\"CacheInternal\", Bi         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-19 16:54

    I have find a solution that maybe is the best for now. If anyone has another, let me know!

      object aspNetCacheInternal = null;
    
      var cacheInternalPropInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);
      if (cacheInternalPropInfo == null)
      {
        // At some point, after some .NET Framework's security update, that internal member disappeared.
        // https://stackoverflow.com/a/45045160
        // 
        // We need to look for internal cache otherwise.
        //
        var cacheInternalFieldInfo = HttpRuntime.Cache.GetType().GetField("_internalCache", BindingFlags.NonPublic | BindingFlags.Static);
    
        if (cacheInternalFieldInfo != null)
        {
          var httpRuntimeInternalCache = cacheInternalFieldInfo.GetValue(HttpRuntime.Cache);
          var httpRuntimeInternalCacheField = httpRuntimeInternalCache.GetType().GetField("_cacheInternal", BindingFlags.NonPublic | BindingFlags.Instance);
    
          if (httpRuntimeInternalCacheField != null)
            aspNetCacheInternal = httpRuntimeInternalCacheField.GetValue(httpRuntimeInternalCache);
        }
      }
      else
      {
        aspNetCacheInternal = cacheInternalPropInfo.GetValue(null, null);
      }
    
      return aspNetCacheInternal;
    

    Regards!

提交回复
热议问题