4

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

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

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

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

在此处输入图像描述

4

3 回答 3

4

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

于 2017-07-18T16:59:23.433 回答
4

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

  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;

问候!

于 2017-10-03T22:23:47.207 回答
2

该内部成员存在于 .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 下运行。

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

于 2017-07-11T22:01:06.420 回答