Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

I'm trying to call a method from my c# hub class but when I try to return a string from the javascript code I get a System.Threading.Tasks.Task instead of a string. I'm wondering how I can edit the javascript method to return an actual string.

Here's the hub class:

public String getMessage(int id)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        var s = hubContext.Clients.All.getMessage(id);


        return s;

    }

JavaScript:

chat.client.getMessage = function (id) {
                for (i = 0; i < messageArr.length; i++) {
                    if (messageArr[i].ID == id) {
                        var s = messageArr[i].ID + messageArr[i].Name + messageArr[i].Content;
                        window.alert(s);
                        return s;
                    }
                }
            }
like image 443
Josh B Avatar asked Mar 16 '26 05:03

Josh B


1 Answers

SignalR does not support return values from clients to hubs. The task returned by Clients.All will complete when all clients have been called.

If you want return values from clients, you will have to manually call the hub with the correct value and match the caller using your own message id.

See the documentation

You can't get a return value from a client method; syntax such as int x = Clients.All.add(1,1) does not work.

like image 133
BradleyDotNET Avatar answered Mar 17 '26 17:03

BradleyDotNET



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!