gpt4 book ai didi

asp.net-core-signalr - SignalR Core 中 SendAsync 和 SendCoreAsync 方法之间的区别?

转载 作者:行者123 更新时间:2023-12-02 01:31:43 34 4
gpt4 key购买 nike

更新到最新版本的 ASP Net Core 和 SignalR core 时,我注意到向客户端发送方法时有两种可用的“发送”方法(以前是 InvokeAsync)。

查看代码注释后,这两个方法的注释是相同的,都继承自 IClientProxy,并且都接受字符串方法、对象 args 和取消标记。

这些方法有什么区别?如果有的话?什么时候应该使用哪个?

最佳答案

引用GitHub中的@anurse :

长话短说:

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 - SignalR Core 中 SendAsync 和 SendCoreAsync 方法之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590347/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com