Roelant M Roelant M - 1 year ago 142
C# Question

SignalR message from client to Server

I have a web api running in azure and windows service locally running on premise. These 'client' logon on the Hub in the web api. When api sends out a command to all clients, all is well received. No problems here.
However, is it possible to send it signal from the same websocket from the client to the server, so from the windows service to the web api? Now my service calls a controller on the web api, but this feels redundant, cause there already would be a websocket available, right?

I am a bit new to signalR, so any help would be appreciate.


Answer Source

You can achieve this with the Invoke method:

var hubConnection = new HubConnection("http://localhost:1235/");
var hub = hubConnection.CreateHubProxy("myHub");

await hubConnection.Start();

hub.Invoke("MethodName", params...);

After creating the hub connection you can invoke a method call from the client with Invoke the first parameter is the methodname (of the method on the server you want to invoke) as string and with params you can send some parameter to the method.
If the invoked method has a return type it can be specified as generic <>:

var result = hub.Invoke<MyReturnType>("MethodName", params...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download