的httpRuntime CacheInternal空引用异常,同时读取用户的会话(反射)

问题描述:

一些更新后,我们的Windows服务器(2008 R2,2012)Asp.net应用抛出错误:的httpRuntime CacheInternal空引用异常,同时读取用户的会话(反射)

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

CacheInternal即将空,不知道为什么?

下面的解决方案是不工作:( Solution

enter image description here

+1

可能重复[什么是NullReferenceException,以及如何解决它?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix -it) – VDWWD

+1

查看HttpRuntime的最新参考源,我没有看到CacheInternal属性。 http://referencesource.microsoft.com/#System.Web/HttpRuntime.cs –

我已经找到解决方案。现在HTTPRuntime类没有CacheInternal属性。为实现上述任务,我创建了一个全局列表,在Session_Start中添加该列表中的会话,并删除Global.asax的Sessions_end函数中的会话。

+0

+1,你知道任何其他的替代方案,因为我的代码依赖于CacheInternal吗?因为我正在尝试处理其他会话 –

,内部成员在.NET 2.0的存在,以及地方.NET 3.5和.NET之间4.6.1消失。这就是为什么你不应该”因为.NET是向后兼容的,所以强制某个运行时版本在运行时不会使用较旧的程序集,如果有新的程序集可用的话:.NET 4.6.1仍然是一个in-p将所有早期版本的花边升级降至4.0。

所以我觉得这个更新无论是从System.Web程序集修补的成员了,还是它4.0是永远不会开始和你的应用程序池某种方式从.NET 2.0更改为.NET 4.0。

当然是不可取的卸载更新,但你可以尝试找到一个删除该成员。然后您必须验证它不是安全更新。

或者强制应用程序在.NET 2.0运行,如果这是可行的。

您也可以尝试找到一个不同的方式来解决原来的问题。

+0

我找到了解决方案。现在HTTPRuntime类没有CacheInternal属性。为实现上述任务,我创建了一个全局列表,在Session_Start中添加该列表中的会话,并删除Global.asax的Sessions_end函数中的会话。 –

我找到一个解决方案,也许是最好的了。如果有人有另一个,让我知道!

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; 

问候!