Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Error while closing the websocket - Invalid Handle

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?

like image 758
wytes Avatar asked Jan 30 '14 18:01

wytes


1 Answers

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;
            }
        }
    }
like image 150
Shachaf.Gortler Avatar answered Nov 11 '22 01:11

Shachaf.Gortler