SignalR Core 中 SendAsync 和 SendCoreAsync 方法的区别?
Difference between SendAsync and SendCoreAsync methods in SignalR Core?
更新到最新版本的 ASP Net Core 和 SignalR core 时,我注意到它们有两个 "send" 方法可用于向客户端发送方法(以前是 InvokeAsync)。
看了代码注释,注释中两个方法是一样的,都继承自IClientProxy,都接受一个字符串方法,对象参数,然后是取消令牌。
这些方法有什么区别?如果有的话?什么时候应该使用哪个?
引用 @anurse
来自 GitHub:
长话短说:
The Core methods should be ignored unless you really know what you're doing.
长篇小说:
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);
更新到最新版本的 ASP Net Core 和 SignalR core 时,我注意到它们有两个 "send" 方法可用于向客户端发送方法(以前是 InvokeAsync)。
看了代码注释,注释中两个方法是一样的,都继承自IClientProxy,都接受一个字符串方法,对象参数,然后是取消令牌。
这些方法有什么区别?如果有的话?什么时候应该使用哪个?
引用 @anurse
来自 GitHub:
长话短说:
The Core methods should be ignored unless you really know what you're doing.
长篇小说:
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);