【问题标题】:SignalR Hub throws NRESignalR Hub 抛出 NRE
【发布时间】:2020-11-17 05:08:03
【问题描述】:

我对空引用异常有疑问,不明白为什么会发生这种情况。这是我的中心

public class UserHub : Hub
    {
        private static List<Connection> Users = new List<Connection>();

        public override async Task OnConnected()
        {
            string id = Context.ConnectionId;
            string userName = Context.User.Identity.Name;

            if (Users == null)
                Users = new List<Connection>();

            if (!Users.ToArray().Any(_ => _.UserName == userName))
                Clients.AllExcept(Context.ConnectionId).onNewUserConnected(id, userName);

            if (!Users.ToArray().Any(x => x.ConnectionId == Context.ConnectionId))
                Users.Add(new Connection { ConnectionId = id, UserName = userName });

            Clients.Caller.onConnected(id, userName, Users.DistinctBy(_ => _.UserName).ToList());

            await base.OnConnected();
        }


        public override async Task OnReconnected()
        {
            string id = Context.ConnectionId;
            string userName = Context.User.Identity.Name;

            if (Users == null)
                Users = new List<Connection>();

            if (!Users.Any(_ => _.UserName == userName))
                Clients.AllExcept(Context.ConnectionId).onNewUserConnected(id, userName);

            if (!Users.Any(x => x.ConnectionId == id))
                Users.Add(new Connection { ConnectionId = id, UserName = userName });

            Clients.Caller.onConnected(id, userName, Users.DistinctBy(_ => _.UserName).ToList());

            await base.OnReconnected();
        }

        public override async Task OnDisconnected(bool stopCalled)
        {
            if (Users == null)
                Users = new List<Connection>();

            var item = Users.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
            if (item != null)
                Users.Remove(item);


            Clients.All.onUserDisconnected(Context.ConnectionId, item.UserName);

            await base.OnDisconnected(stopCalled);
        }
    }

问题出现在 onConnected 方法中,在这一行:

if (!Users.ToArray().Any(_ => _.UserName == userName))

我尝试在上面添加对 null 的检查,但它不起作用。怎么了?该问题随机出现。

堆栈跟踪:

at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at App.Web.Hubs.UserHub.<OnConnected>d__1.MoveNext() in E:\Company\App\App\Presentation\App.Web\Hubs\UserHub.cs:line 49
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
...

【问题讨论】:

  • 那么 is 究竟是什么?用户?列表中的条目?你有堆栈跟踪吗?
  • 我相信如果列表中的条目为 null 并且用户有错,那没关系。我已将堆栈跟踪添加到初始消息
  • 好吧,看来_ 在您的列表中为空。
  • 怎么回事,我没有在 Users 数组中添加空值?

标签: c# asp.net-web-api signalr signalr-hub


【解决方案1】:

您的代码不是线程安全的,因此您在访问Users 列表时会看到各种问题。多个连接可以同时启动或关闭,它们都在访问静态 Users 属性。访问列表时考虑使用并发数据结构或锁。

【讨论】:

  • 或者,不要使用静态字段。
  • 静态字段没问题。 Hub 是一个瞬态类,所以如果你想在客户端和请求之间存储状态,你需要一个静态字段,或者一个为你存储状态的服务(这也需要是线程安全的)。
  • 这就是我的意思:使用服务要好得多,尤其是考虑到 DI 现在在 .NET 中无处不在。当然,正如您所说,您仍然需要该服务中的线程安全数据结构。
猜你喜欢
  • 2023-02-16
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多