Search code examples
c#asp.net-mvcwebsocket

websockets: Invalid session ID. The session may have been already closed


we are try to connect biometric device using websockets , it works fine in asp.net. we use same class library in mvc not working getting we 'Invalid session ID. The session may have been already closed.

DeviceLoginManager Class:

    public static OnlineDevice[] GetOnlineDevices() //error
    {
        List<OnlineDevice> online = new List<OnlineDevice>();

        foreach(Guid guid in SessionRegistry.GetKeys())
        {
            if (SessionRegistry.GetSession(guid).loggedIn)
            {
                online.Add(new OnlineDevice { session = SessionRegistry.GetSession(guid) });
            }

            const int KeepAliveTimeout = 10 * 60 * 1000;
            SBWebSocketHandler aSession = SessionRegistry.GetSession(guid);
            if (aSession.keepAliveMsgSupported &&
                aSession.keepAliveRecvedTime < Environment.TickCount - KeepAliveTimeout)
            {
                aSession.Close();
            }
        }

        return online.ToArray();
    }

SessionRegistry Class

internal static object _monitor = new object(); internal static Dictionary<Guid, SBWebSocketHandler> _sessions = new Dictionary<Guid, SBWebSocketHandler>(); private static RandomNumberGenerator _random = RandomNumberGenerator.Create(); /// <summary> /// Retrieves an open session from its ID. /// </summary> /// <param name="guid">Session ID</param> /// <returns>A session object if matching ID was found; otherwise null</returns> public static SBWebSocketHandler GetSession(Guid guid) { lock (_monitor) { SBWebSocketHandler result; if (_sessions.TryGetValue(guid, out result)) return result; else { // return null; throw new WebDeviceException("Invalid session ID. The session may have been already closed."); } } } internal static Guid AddSession(SBWebSocketHandler handler) { lock (_monitor) { byte[] guidBytes = new byte[16]; Guid guid; do { _random.GetBytes(guidBytes); guid = new Guid(guidBytes); } while (_sessions.ContainsKey(guid)); _sessions.Add(guid, handler); return guid; } } public static Dictionary<Guid, SBWebSocketHandler>.KeyCollection GetKeys() { lock (_monitor) { return _sessions.Keys; } }

SBWebSocketHandler Class:

private IDeviceLoginManager _loginManager; public Guid SessionId { get { return m_guid; } }

public override void OnOpen() {

   _loginManager = (IDeviceLoginManager)
       Type.GetType(ConfigurationManager.AppSettings["smackbio.websocketsdk.deviceLoginManager"])
       .GetConstructor(new Type[0])
       .Invoke(new object[0]);

   m_guid = SessionRegistry.AddSession(this);

   registerMsgRecved = false;
   loggedIn = false;
   keepAliveMsgSupported = false;

} enter image description here enter image description here enter image description here

enter image description here


Solution

  • Add GenericHandler.ashx in your project