Kayum
Kayum

Reputation: 21

SignalR does not handles received hub invocation

I have SignalR hub with several endpoints, all endpoint works except one. Hub is receiving message but cannot handle it.

//I set additional parameters to Logging configuration in application.json
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.AspNetCore.SignalR": "Debug",//this 
      "Microsoft.AspNetCore.Http.Connections": "Debug"//and this one
    }
  }

logs when client calls endpoint ConsumeCandidate : dbug: Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher[1] Received hub invocation: InvocationMessage { InvocationId: "2", Target: "ConsumeCandidate", Arguments: [ lolkek, 581dc267-d2ff-403f-924c-cd6dba40535b ], StreamIds: [ ] }.

Server has no problems with receiving message, but hub doesn't invoke necessary method.

Hub:

public class LobbyHub(ILobbyService lobbyService, ILogger<LobbyHub> logger, IGameService gameService,
    IElectionManager electionManager) : Hub
{
    // this method should be invoked
    public void ConsumeCandidate(string lobbyName, string userClientId)
    {
        logger.LogInformation($"Chancellor candidate received from: {userClientId}");
    }

    //For instance hub is able to invoke this one
    public async Task StartGame(string lobbyName, string userClientId)
    { 
        await gameService.StartRound(lobbyName, userClientId);
    }
}

app.MapHub<LobbyHub>("/lobbyhub");

As you can see, it cannot be any naming issues, also note that all other methods of this hub are working well, connection isn't lost, the same client can call other endpoints after ConsumeCandidate and there are no troubles with that. There are no logs from ConsumeCandidate method when message from client is received.

I checked all configuring things, other working endpoints can be prof of there is nothing wrong with configs(I mean hub is mapped and SignalR is registered in DI). I tried to add [HubMethodName("ConsumeCandidate")] to method, but nothing happened. I tried tried to debug ConsumeCandidate method, but due to it can't be called, debug didn't help. All I can say that server is receiving message but does not process it.

//I added this option to see errors, but there are no errors after request is received.

builder.Services.AddSignalR().AddHubOptions<LobbyHub>(options =>
{
    options.EnableDetailedErrors = true;
});

Client code:

const invokeMethod = async (methodName, ...args) => {
    console.log("Invoking method:", methodName, "\nwith args:", args);
    console.log("Connection state:", connection.state);
    return connection.invoke(methodName, ...args);
};

function setChoosingCandidatesButton(candidates) {
    candidates.allCandidates.forEach((candidate) => {
        const candidateButton = document.getElementById('s' + candidate.index);
        candidateButton.textContent = "choose";
        candidateButton.dataset.id = candidate.userId + " " + candidates.lobbyName;
        candidateButton.onclick = (e) => {
            let data = e.target.dataset.id.split(" ");
            invokeMethod("ConsumeCandidate", data[1], data[0]);
        };
    });
}

enter image description here

I added second hub to test the method:

public class GameHub(ILogger<GameHub> logger,
IElectionManager electionManager) : Hub
{
    public async Task ConsumeCandidate(string lobbyName, string userClientId)
    {
        logger.LogInformation($"Chancellor candidate received from: {userClientId}");
    }
}

Logs:

dbug: Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher[1]
      Received hub invocation: InvocationMessage { InvocationId: "0", Target: "ConsumeCandidate", Arguments: [ lolkek, 581dc267-d2ff-403f-924c-cd6dba40535b ], StreamIds: [  ] }.
info: SecretHitler.WebApp.Hubs.GameHub[0]
      Chancellor candidate received from: 581dc267-d2ff-403f-924c-cd6dba40535b

Also overwrote OnDisconnectedAsync

//This method from LobbyHub
    public override Task OnDisconnectedAsync(Exception? exception)
    {
        logger.LogInformation($"Client disconnected {Context.ConnectionId}");
        return base.OnDisconnectedAsync(exception);
    }

there are no exceptions or logs from OnDisconnectedAsync.

Upvotes: 2

Views: 67

Answers (0)

Related Questions