'错误:服务器在关闭时返回错误:连接因错误而关闭。 InvalidOperationException:可为 Null 的对象必须有一个值

问题描述 投票:0回答:1

我在通过 SignalR 建立连接时遇到错误。当我建立 SignalR 连接时,连接开始后,它立即断开连接并抛出错误。

错误:连接因错误而断开连接‘错误:服务器在关闭时返回错误:连接因错误而关闭。 InvalidOperationException:可为 Null 的对象必须有一个值。’。

public startConnection(): void {
    this.hubConnection = new HubConnectionBuilder()
      .withUrl(`${this.API_URL}/interviewRoundHub`).withAutomaticReconnect([5000])
      .build();
    if (this.hubConnection && this.hubConnection.state === "Disconnected") {
      this.hubConnection.start().then(() => {
        console.log('SignalR connection started.');
        this.getStatus()
        //this.candidateList?.unshift(this.candidateList[0]);
      }).catch(error => {
        console.error('SignalR connection error:', error);
      });
    }
  }

  getStatus(){
      this.hubConnection?.on('FrontEndMethod', (assessmentStatus, candidateInviteId) => {
        console.log(assessmentStatus);
        console.log(candidateInviteId);
        this.message.assess = assessmentStatus;
        this.message.cand = candidateInviteId;
        this.receivedStatus.next(assessmentStatus);
        this.receivedCandidateId.next(candidateInviteId);
        this.message1.next(this.message)
      });
  }
public override async Task OnConnectedAsync()
{
var currentUserId = _currentUser.GetId();
var currentUser = await _userManager.GetByIdAsync(currentUserId);
var currentUserRoles = await _userManager.GetRolesAsync(currentUser);
if (!_connectionsMap.Any(u => u.Key == currentUserId))
{
_connectionsMap.Add(currentUserId, Context.ConnectionId);
}
if (currentUserRoles.Count > 0 && currentUserRoles.Any(x => x == _stringConstant.TeamAdmin))
{
_teamAdminMap.Add(currentUserId, Context.ConnectionId);
}
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
var userId = _currentUser.GetId();
_connectionsMap.Remove(userId);
if (_teamAdminMap.ContainsKey(userId))
{
_teamAdminMap.Remove(userId);
}
return base.OnDisconnectedAsync(exception);
}

enter image description here

我期待 SignalRconnection。

angular signalr signalr-hub asp.net-core-signalr
1个回答
0
投票

我们可以从浏览器中找到日志,它在内部崩溃了

getStatus()
,所以我们需要检查后端的FrontEndMethod函数。

我们还可以启用服务器端日志记录来检查更多详细信息。

通常

InvalidOperationException: Nullable object must have a value.
表示
FrontEndMethod
内的模型有问题。

© www.soinside.com 2019 - 2024. All rights reserved.