Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SendAsync and SendCoreAsync methods in SignalR Core?

Tags:

When updating to the latest version of ASP Net Core and SignalR core, I noticed their are two "send" methods available when sending methods to a client (what used to be InvokeAsync).

After looking at the code comments, both methods are identical in comments, both inherit from IClientProxy, and both accept a string method, object args and then a cancellation token.

What are the differences in these methods? If any? and which should be used when?

like image 799
FasettoAndy Avatar asked Jul 30 '18 09:07

FasettoAndy


People also ask

What are hubs in SignalR?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.

How do I add a class to hub SignalR?

Or you can also add SignalR to a project by opening the "Tools" | "Library Package Manager" | "Package Manager Console" and running the command: "install-package Microsoft. AspNet. SignalR". Now again add the SignalR class.

When should I use SignalR?

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements long polling to retrieve new data, it is a candidate for using SignalR.

What is SignalR connection?

SignalR includes automatic connection management, it provides the facility to broadcast messages to all connected clients or to a specific client. The connection between the client and server is persistent while in HTTP it isn't persistent.


1 Answers

Quoting @anurse from GitHub:

Long story short:

The Core methods should be ignored unless you really know what you're doing.

Short story long:

We started with SendAsync, which takes an array of arguments to send:

public void SendAsync(string method, object[] args);  Clients.All.SendAsync("Method", new object[] { arg1, arg2, arg3 }); 

Obviously it's a pain to have to create an array every time. The easy fix for that would be to use params:

public void SendAsync(string method, params object[] args);  Clients.All.SendAsync("Method", arg1, arg2, arg3); 

However, that falls apart when you actually want to send an array as a single argument

public void SendAsync(string method, params object[] args);  var arg1 = new object[] { a, b, c };  Clients.All.SendAsync("Method", arg1);  // C# 'params' expands this into the below  Clients.All.SendAsync("Method", a, b, c); 

So instead of sending a single argument that is an array a, b, c, we've sent each of those as separate arguments. This was confusing users.

So we removed the params from it and instead we generate a whole bunch of extension methods that support multiple arguments:

public void SendAsync(string method, object[] args); public void SendAsync(string method, object arg1) => SendAsync(method, new object[] { arg1 }); public void SendAsync(string method, object arg1, object arg2) => SendAsync(method, new object[] { arg1, arg2 }); // ... etc ... 

But there's still ambiguity when you have code like this:

public void SendAsync(string method, object[] args); public void SendAsync(string method, object arg1) => SendAsync(method, new object[] { arg1 });  var arg = new object[] { a, b, c }  Clients.All.SendAsync("method", arg); 

Again, the overload that takes an object[] will be chosen (see this illustration on SharpLab).

So, we renamed the one that takes an array to SendCoreAsync:

public void SendCoreAsync(string method, object[] args); public void SendAsync(string method, object arg1) => SendCoreAsync(method, new object[] { arg1 });  var arg = new object[] { a, b, c }  // No ambiguity here! Clients.All.SendAsync("method", arg); 
like image 150
emix Avatar answered Sep 18 '22 13:09

emix