1

I have ASP NET Core web API and use SignalR. I have 3 separate hubs, and MessageHub one of them. Here is the code:

public class MessageHub : Hub
{
    public MessageHub() { }

    static List<UserDetail> ConnectedUsers = new List<UserDetail>();

    public override Task OnConnectedAsync()
    {
        var userId = Context.UserIdentifier;
        var id = Context.ConnectionId;

        ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserID = userId });

        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
        var id = Context.ConnectionId;

        UserDetail userDetail = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == id);

        if (userDetail != null)
        {
            ConnectedUsers.Remove(userDetail);
        }

        return base.OnDisconnectedAsync(exception);
    }
}

UserDetails:

public class UserDetail
{
    public string ConnectionId { get; set; }
    public string UserID { get; set; }
}

I using overrided method for connecting and disconnecting, and it works well, but the problem is that sometimes, after an hour or day, problem appears. I start get this error in logger:

|ERROR|Microsoft.AspNetCore.SignalR.HubConnectionHandler|Error when dispatching 'OnConnectedAsync' on hub. System.NullReferenceException: Object reference not set to an instance of an object.

What can be the issue? Restarting fix this problem for a while but then it comes back.

zhulien
  • 5,145
  • 3
  • 22
  • 36
Mateech
  • 1,010
  • 1
  • 11
  • 26
  • @IbraHimM.Nada when error arrives other new connected users cant connect to hub, all of them get this error – Mateech Mar 01 '21 at 12:28
  • @IbraHimM.Nada yes, for now it have small amount of traffic, but it should grow in future – Mateech Mar 01 '21 at 12:29
  • something on OnConnectedAsync is null , and try to put await before SendAsync i think it has something to do with it – IbraHim M. Nada Mar 01 '21 at 12:31
  • Q: What is the purpose of assigning local UserDetail CurrentUser = ? You don't use CurrentUser for any purpose inside method OnConnectedAsync(), you could remove the statement.. – Goodies Mar 01 '21 at 15:38
  • A safer solution is to use a database to store your connected users, instead of relying (for hours) on static memory data. Static data in an ASP.NET application could be reset, session management may be involved, assemblies can be reloaded.. Refer to this question https://stackoverflow.com/questions/8919095/lifetime-of-asp-net-static-variable – Goodies Mar 01 '21 at 15:47
  • You aren't accessing ConnectedUsers in a thread safe manner, multiple threads can be using it at once in your current example, so a null ref isn't too surprising. – Brennan Mar 02 '21 at 02:08
  • @Brennan what are you suggesting to do for access `ConnectedUsers` – Mateech Mar 02 '21 at 12:09
  • Looks like you are facing issue with app domain recycling -https://stackoverflow.com/q/17235193/3576971. Hope this gives some insight. – mbshambharkar Mar 02 '21 at 14:54
  • Use a lock, or use a concurrent collection like ConcurrentDictionary. – Brennan Mar 02 '21 at 16:01

0 Answers0