SignalR Error while closing the websocket - Invalid Handle

6

This SignalR problem occurs:

Error while closing the websocket: System.Net.WebSockets.WebSocketException (0x80070006): The handle is invalid

I think that the problem is linked to this code:

var currentHub = GlobalHost.ConnectionManager.GetHubContext<HubManager>();
currentHub.Groups.Remove(userConnectionId, roomName);

How can it be fixed?

c#
exception
websocket
signalr
asked on Stack Overflow Jan 30, 2014 by wytes • edited Apr 30, 2019 by ΩmegaMan

1 Answer

1

I have had the same problem , This started happening when I added an SQL Backplane to signalR,

It has to do with the "Freshness" of the hub context what I did is this :

    /// <summary>
    /// In case a backplane  is used (in case of load balancer) , the instance should always be taken fresh
    /// if no backplane is used no need to refresh the instance on each invocation
    public class HubContextService 
    {
        bool BackplaneUsed { get; set; }
        IHubContext _context = null;

        public  HubContextService(bool isBackPlaneUsed = true)
        {
            BackplaneUsed = isBackPlaneUsed;
        }

        public IHubContext HubContext
        { 
            get
            {
                if (BackplaneUsed)
                {
                    return GlobalHost.ConnectionManager.GetHubContext<HubManager>();
                }
                else
                {
                    if (_context == null)
                    {
                        _context = GlobalHost.ConnectionManager.GetHubContext<HubManager>(); 
                    }
                    return _context;
                }
            }
            set 
            {
                _context = value;
            }
        }
    }
answered on Stack Overflow Apr 28, 2014 by Shachaf.Gortler

User contributions licensed under CC BY-SA 3.0