【问题标题】:NULL reference exception while reading user sessions (Reflection)读取用户会话时出现 NULL 引用异常(反射)
【发布时间】:2012-11-07 02:15:53
【问题描述】:

我已经使用参考Reading All Users SessionGet a list of all active sessions in ASP.NET 实现了读取活动会话的代码。

Private List<String> getOnlineUsers()
{
    List<String> activeSessions = new List<String>();
    object obj = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
    object[] obj2 = (object[])obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
    for (int i = 0; i < obj2.Length; i++)
    {
        Hashtable c2 = (Hashtable)obj2[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj2[i]);
        foreach (DictionaryEntry entry in c2)
        {
            object o1 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
            if (o1.GetType().ToString() == "System.Web.SessionState.InProcSessionState")
            {
                SessionStateItemCollection sess = (SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                if (sess != null)
                {
                    if (sess["loggedInUserId"] != null)
                    {
                        activeSessions.Add(sess["loggedInUserId"].ToString());
                    }
                }
            }
        }
    }
    return activeSessions;
}

它在本地系统中运行良好(在 Windows XP 和 Windows 7 中)。当我在 Windows server 2003(IIS 版本 6)中托管应用程序时,它在该行中给出了 NULL 对象引用错误

object[] obj2 = (object[])obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);

这是否与权限问题或与 IIS 相关的信任级别设置有关?请让知道任何人遇到过这样的问题。任何帮助都是非常值得赞赏的。

【问题讨论】:

  • 这3个地方安装的是什么版本的.NET?你的目标是什么版本?
  • Framework 3.5 存在于服务器中。
  • @MarcGravell 两者都在场。

标签: c# asp.net iis-6 hosting reflectionpermission


【解决方案1】:

听起来 2003 上运行的 .NET 版本(或更新)与 XP / Win7 上的不同,尽管它也可能只是特定于平台的差异。如果是权限/信任,您会看到异常。相反,似乎更有可能是:_caches 不存在 在 2003 机器上的任何版本上。如果您使用反射来访问私有状态:您应该完全期望它在版本/更新/平台/随心所欲/等之间爆炸。

调查:

  • 检查obj是否为null
  • 检查obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance)是否为null

(其中任何一个都可能导致您引用的行出现此异常)

【讨论】:

  • 好吧...我猜可能是这样。因为相同的代码在其中一个客户端服务器(MS server 2003)中工作。
  • 托管为虚拟目录/网站 - 这有关系吗?
  • @techdo 回答最后一个问题的唯一方法是比较......你可能最适合尝试它
【解决方案2】:

对于您的业务案例,asp.net 中有应用程序状态变量。它类似于会话状态,但对所有用户请求都是可见的。

【讨论】:

    【解决方案3】:

    我知道这是一个旧线程,但这可能会节省一些时间。要检查的另一件事是 obj 的类型为 System.Web.Caching.CacheMultiple。我遇到了同样的问题,这是@Marc Gravell 建议的特定于平台的问题。原来,在 Windows 2003 服务器上,obj 是 System.Web.Caching.CacheSingle 类型,在尝试获取“_caches”的值时出现空引用异常。

    如果是这种情况,您仍然可以使用(Hashtable)obj.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj); 获取活动会话列表

    【讨论】:

      【解决方案4】:

      如果 _caches 为 NULL,请尝试使用 _cachesRefs。下面的函数将返回所有多个 Windows 版本(包括 Windows Server)的所有用户会话集合。

      有效。

      public List<SessionStateItemCollection> GetAllUserSessions() {
      
      List<Hashtable> hTables = new List<Hashtable>();
      
      PropertyInfo propInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);
      
      object CacheInternal = propInfo.GetValue(null, null);
      
      dynamic fieldInfo = CacheInternal.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);
      
      if (fieldInfo != null) {
          object[] _caches = (object[])fieldInfo.GetValue(CacheInternal);
          for (int i = 0; i <= _caches.Length - 1; i++) {
              Hashtable hTable = (Hashtable)_caches(i).GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_caches(i));
              hTables.Add(hTable);
          }
      } else {
          fieldInfo = CacheInternal.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
          dynamic cacheRefs = fieldInfo.GetValue(CacheInternal);
          foreach (void cacheRef_loopVariable in cacheRefs) {
              cacheRef = cacheRef_loopVariable;
              dynamic target = cacheRef.Target;
              fieldInfo = target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance);
              Hashtable hTable = fieldInfo.GetValue(target);
              hTables.Add(hTable);
          }
      }
      
      List<SessionStateItemCollection> sessionlist = new List<SessionStateItemCollection>();
      
      foreach (void hTable_loopVariable in hTables) {
          hTable = hTable_loopVariable;
          foreach (DictionaryEntry entry in hTable) {
              object value = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
              if (value.GetType().ToString() == "System.Web.SessionState.InProcSessionState") {
                  SessionStateItemCollection sCollection = (SessionStateItemCollection)value.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(value);
                  if (sCollection != null)
                      sessionlist.Add(sCollection);
              }
          }
      }
      
      return sessionlist;
      

      }

      【讨论】:

      • voids 将在 foreachs(void -> var
      【解决方案5】:

      我尝试了 Paully 的解决方案,该解决方案在某些方面无法编译,并在其他方面导致运行时错误。无论如何,受到他的建议的启发(非常感谢!我的投票赞成),我自己来了,它编译并获得了预期的数据。

      另外,我正在返回一个 IEnumerable 并且我正在使用“yield return”,这使得它对于大列表(一种数据的延迟加载)更具性能。就是这样:

      public static System.Collections.Generic.IEnumerable<SessionStateItemCollection> GetAllUserSessions()
      {
          List<Hashtable> hTables = new List<Hashtable>();
          object obj = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
          dynamic fieldInfo = obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);
      
          //If server uses "_caches" to store session info
          if (fieldInfo != null)
          {
              object[] _caches = (object[])fieldInfo.GetValue(obj);
              for (int i = 0; i <= _caches.Length - 1; i++)
              {
                  Hashtable hTable = (Hashtable)_caches[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_caches[i]);
                  hTables.Add(hTable);
              }
          }
          //If server uses "_cachesRefs" to store session info
          else
          {
              fieldInfo = obj.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
              object[] cacheRefs = fieldInfo.GetValue(obj);
              for (int i = 0; i <= cacheRefs.Length - 1; i++)
              {
                  var target = cacheRefs[i].GetType().GetProperty("Target").GetValue(cacheRefs[i], null);
                  Hashtable hTable = (Hashtable)target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(target);
                  hTables.Add(hTable);
              }
          }
      
          foreach (Hashtable hTable in hTables)
          {
              foreach (DictionaryEntry entry in hTable)
              {
                  object o1 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
                  if (o1.GetType().ToString() == "System.Web.SessionState.InProcSessionState")
                  {
                      SessionStateItemCollection sess = (SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                      if (sess != null)
                           yield return sess;
                  }
              }
          }
      }
      

      【讨论】:

      • 太棒了!是的,我是手写的,可能有错别字。祝你好运!
      【解决方案6】:

      截至 2010 年 2 月,在 IIS 10.0 (Windows 10) 上进行测试时,上述解决方案并不完全奏效,因此我将以下对我有用的解决方案组合在一起。它建立在awerdan 的答案之上,并结合了Diogo Damiani 的答案。

          // attempt to get Asp.Net internal cache
          // adapted from https://stackoverflow.com/a/46554310/1086134
          private static object getAspNetInternalCacheObj ()
          {
              object aspNetCacheInternal = null;
      
              PropertyInfo cacheInternalPropInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);
              if (cacheInternalPropInfo != null)
              {
                  aspNetCacheInternal = cacheInternalPropInfo.GetValue(null, null);
                  return aspNetCacheInternal;
              }
      
              // 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)
                  return null;
      
              var httpRuntimeInternalCache = cacheInternalFieldInfo.GetValue(HttpRuntime.Cache);
              var httpRuntimeInternalCacheField = httpRuntimeInternalCache.GetType().GetField("_cacheInternal", BindingFlags.NonPublic | BindingFlags.Instance);
              if (httpRuntimeInternalCacheField == null)
                  return null;
      
              aspNetCacheInternal = httpRuntimeInternalCacheField.GetValue(httpRuntimeInternalCache);
              return aspNetCacheInternal;
          }
      
          // adapted from https://stackoverflow.com/a/39422431/1086134
          private static IEnumerable<System.Web.SessionState.SessionStateItemCollection> getAllUserSessions()
          {
              List<Hashtable> hTables = new List<Hashtable>();
              object obj = getAspNetInternalCacheObj();
              if (obj == null)
                  yield break;
      
              dynamic fieldInfo = obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);
              //If server uses "_caches" to store session info
              if (fieldInfo != null)
              {
                  object[] _caches = (object[])fieldInfo.GetValue(obj);
                  for (int i = 0; i <= _caches.Length - 1; i++)
                  {
                      Hashtable hTable = (Hashtable)_caches[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_caches[i]);
                      hTables.Add(hTable);
                  }
              }
              //If server uses "_cachesRefs" to store session info
              else
              {
                  fieldInfo = obj.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
                  object[] cacheRefs = fieldInfo.GetValue(obj);
                  for (int i = 0; i <= cacheRefs.Length - 1; i++)
                  {
                      var target = cacheRefs[i].GetType().GetProperty("Target").GetValue(cacheRefs[i], null);
                      Hashtable hTable = (Hashtable)target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(target);
                      hTables.Add(hTable);
                  }
              }
      
              foreach (Hashtable hTable in hTables)
              {
                  foreach (DictionaryEntry entry in hTable)
                  {
                      object o1 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
                      if (o1.GetType().ToString() == "System.Web.SessionState.InProcSessionState")
                      {
                          System.Web.SessionState.SessionStateItemCollection sess = (System.Web.SessionState.SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                          if (sess != null)
                              yield return sess;
                      }
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2017-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        • 2013-01-13
        • 2021-08-06
        相关资源
        最近更新 更多