ASP.NET中的实时signalR错误.ContainsKey

时间:2013-10-25 23:51:19

标签: real-time signalr signalr-hub

我尝试与SignalR ised和ASP.Net(VB)聊天 运行时,与第一个用户完美配合,但与另一个用户登录时会出错:

错误:溢出

请帮忙,我是实时应用的新手

Public Overrides Function OnConnected() As Task
        Dim myRoomId = "XXXX"
        Dim myUserId = IdentityUser.UserCode 
        SyncLock connections
            If Not connections.ContainsKey(myRoomId) Then
                connections(myRoomId) = New Dictionary(Of Integer, List(Of String))()
            End If
            If Not connections(myRoomId).ContainsKey(myUserId) Then ' <<<<<--- Error
                connections(myRoomId)(myUserId) = New List(Of String)()
            End If
            connections(myRoomId)(myUserId).Add(Me.Context.ConnectionId)
        End SyncLock
        Return MyBase.OnConnected()
End Function

1 个答案:

答案 0 :(得分:0)

您正在锁定对象&#34;连接&#34;并试图在SyncLock中更改它,这是不允许的。

使用空对象锁定SyncLock,它应该是公共的并且是共享的。 此外,您不应该使用&#34; OnConnect()&#34;事件因为它会在hub方法调用中每次触发

像这样

Private Shared _lock_connections As New Object

Public Overrides Function OnConnected() As Task
Dim myRoomId = "XXXX"
Dim myUserId = IdentityUser.UserCode
SyncLock _lock_connections
    If Not connections.ContainsKey(myRoomId) Then
        connections(myRoomId) = New Dictionary(Of Integer, List(Of String))()
    End If
    If Not connections(myRoomId).ContainsKey(myUserId) Then ' <<<<<--- Error
        connections(myRoomId)(myUserId) = New List(Of String)()
    End If
    connections(myRoomId)(myUserId).Add(Me.Context.ConnectionId)
End SyncLock
Return MyBase.OnConnected()
End Function