Call to SignalR Hub using proxy.Invoke hangs

I'm having a hang issue with Microsoft.AspNet.SignalR.Client v2.4, I call a hub method from Xamarin android client and it hangs. Client code is:

public List<string> ConnectedUsers()
{
    // Hangs on this line
    List<string> Users = _proxy.Invoke<List<string>>("getConnectedUsers").Result;

    return Users;
}

Hub method code is:

public List<string> getConnectedUsers()
{
    List<string> UserNames = new List<string>();

    foreach (var ConnectedUser in Users)
    {
        UserNames.Add(ConnectedUser.Name );
    }

   return UserNames;
}

The call works in a html form using jquery/javascript, I set a click event for a button:
Clicking the get users button executes the following:

            $('#getusers').click
            (
                function () 
                {
                    chat.server.getConnectedUsers().done(function (result) {
                        alert(result);
                    });
                }
            );

This works fine, I the result I expect.

Posts

  • PrescottChartierPrescottChartier USMember ✭✭

    Finally got it to work after a week of research. After reading this document (https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskresult-and-taskwait) provided by David Fowler on the GitHub site I changed my code as follows:

    On the client:
    From:

    public List ConnectedUsers()
    {
    // Hangs on this line
    List Users = _proxy.Invoke<List>("getConnectedUsers").Result;

    return Users;
    

    }

    To:

    public async Task <List<string>> ConnectedUsers()
    {
    
        List<string> Users = await _proxy.Invoke<List<string>>("getConnectedUsers");
    
        return Users;
    }
    

    The call to the ConnectedUsers function in Client.cs was changed as well:
    From:

            List<string> userList = client.ConnectedUsers();
    

    To:
    List userList = await client.ConnectedUsers();

    No changes to the hub code were necessary. It now works as expected.

Sign In or Register to comment.