Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

var obj_1 = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static); 

CacheInternal is coming null, dont know why ?

following solution is not working :( Solution

enter image description here

like image 874
sarvesh kushwaha Avatar asked Dec 24 '22 16:12

sarvesh kushwaha


2 Answers

I have find the solution. Now HTTPRuntime class doesnt have CacheInternal Property.So to achive the above task I have created a global list adding sessions in that list in Session_Start and removing the sessions in Sessions_end functions of Global.asax.

like image 186
sarvesh kushwaha Avatar answered Mar 08 '23 22:03

sarvesh kushwaha


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!

like image 22
Diogo Damiani Avatar answered Mar 08 '23 22:03

Diogo Damiani