【问题标题】:HttpRuntime CacheInternal NULL reference exception while reading user sessions (Reflection)读取用户会话时出现 HttpRuntime CacheInternal NULL 引用异常(反射)
【发布时间】:2017-12-15 20:52:27
【问题描述】:

在我们的 windows 服务器(2008R2 ,2012) 上进行一些更新后,Asp.net 应用程序抛出错误:

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

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

以下解决方案不起作用:( Solution

【问题讨论】:

标签: c# asp.net reflection iis-7 reflectionpermission


【解决方案1】:

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

【讨论】:

  • +1 ,由于我的代码依赖于 CacheInternal,您是否知道任何其他替代方案?因为我正在尝试处理其他会话
【解决方案2】:

我找到了一个可能是目前最好的解决方案。如果有人有其他的,请告诉我!

  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;

问候!

【讨论】:

    【解决方案3】:

    该内部成员存在于 .NET 2.0 中,但在 .NET 3.5 和 .NET 4.6.1 之间消失了。这就是为什么您不应该使用反射来依赖非公共成员的原因。它们可以随时消失或重命名。

    因为 .NET 是向后兼容的,如果有更新的程序集可用,强制某个运行时版本将不会在运行时使用旧程序集:.NET 4.6.1 仍然是所有早期版本到 4.0 的就地升级。

    因此,我认为此更新要么将成员从 System.Web 程序集中删除,要么从 4.0 开始,您的应用程序池不知何故从 .NET 2.0 更改为 .NET 4.0。

    当然不建议卸载更新,但您可以尝试找到删除此成员的更新。然后,您必须验证它不是安全更新。

    如果可行,也可以强制应用程序在 .NET 2.0 下运行。

    您也可以尝试寻找不同的方法来解决原来的问题。

    【讨论】:

    • 我找到了解决方案。现在 HTTPRuntime 类没有 CacheInternal 属性。所以为了完成上述任务,我创建了一个全局列表,在 Session_Start 的该列表中添加会话,并在 Global.asax 的 Sessions_end 函数中删除会话。
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2021-08-06
    相关资源
    最近更新 更多