C# 信号集线器

C# 信号集线器,c#,asp.net-web-api,signalr,signalr-hub,C#,Asp.net Web Api,Signalr,Signalr Hub,我有一个空引用异常的问题,无法理解为什么会发生这种情况。这是我的中心 public class UserHub : Hub { private static List<Connection> Users = new List<Connection>(); public override async Task OnConnected() { string id = Context.Connec

我有一个空引用异常的问题,无法理解为什么会发生这种情况。这是我的中心

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);
        }
    }
我尝试在上面添加空值检查,但它不起作用。发生了什么?这个问题是随机发生的

堆栈跟踪:

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)
...
at System.Linq.Enumerable.Any[TSource](IEnumerable`1源,Func`2谓词)
在E:\Company\App\App\Presentation\App.Web\Hubs\UserHub.d\u 1.MoveNext()中的App.Web.Hubs.UserHub.d\u 1.MoveNext()处:第49行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
...

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

那么,NULL到底是什么?用户?名单上的一个条目?你有堆栈跟踪吗?我相信如果列表中的条目为空并且smth对用户有错误,这是可以的。我已将stacktrace添加到初始messageWell,您的列表中的
\uuu
似乎为空。这是怎么发生的,我没有将空值添加到用户数组中?或者,不要使用静态字段。静态字段可以。Hub是一个临时类,因此如果您想在客户端和请求之间存储状态,您需要一个静态字段,或者一个为您存储状态的服务(也需要是线程安全的)。这就是我的意思:使用服务要好得多,特别是考虑到DI现在在.NET中无处不在。当然,正如您所说,您仍然需要该服务中的线程安全数据结构。
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)
...