gpt4 book ai didi

c# - SignalR 2 StartAsync 永远不会返回

转载 作者:行者123 更新时间:2023-11-30 18:14:24 25 4
gpt4 key购买 nike

在类的后续部分中,StartAsync 永远不会返回。

有什么想法吗?服务器似乎工作正常,并且可以与 Javascript 客户端一起使用。

SignalR 客户端版本为 v1.0.0-rc1-final

    public HubUtil(string baseUrl) //string clientId
{
connection = new HubConnectionBuilder()
.AddJsonProtocol()
.WithUrl(baseUrl) // baseUrl is "https://hostname/hubname"
.Build();

connection.Closed += Connection_Closed;
StartIfNeededAsync();
}

private Task Connection_Closed(Exception arg)
{
return StartIfNeededAsync();
}

public async Task StartIfNeededAsync()
{
if (_connectionState == ConnectionState.Connected)
{
return;
}

try
{
await connection.StartAsync(); // Never connects
_connectionState = ConnectionState.Connected;
}
catch (Exception ex)
{
_connectionState = ConnectionState.Faulted;
throw;
}
}

在基本的控制台应用程序中 hubutil 是这样调用的:

    static void Main(string[] args)
{
var hub = new HubUtil("https://host/hubname");
hub.Invoke("checkin", "id", "");
}

最佳答案

可能试图同时做太多事情。

从构造函数中删除 StartIfNeededAsync

public HubUtil(string baseUrl) {
connection = new HubConnectionBuilder()
.AddJsonProtocol()
.WithUrl(baseUrl) // baseUrl is "https://hostname/hubname"
.Build();

connection.Closed += Connection_Closed;
}

private Task Connection_Closed(Exception arg) {
return StartIfNeededAsync();
}

public async Task StartIfNeededAsync() {
if (_connectionState == ConnectionState.Connected) {
return;
}

try {
await connection.StartAsync();
_connectionState = ConnectionState.Connected;
} catch (Exception ex) {
_connectionState = ConnectionState.Faulted;
throw;
}
}

//...

并使其成为显式调用,同时考虑 SSL。

//Handle TLS protocols
System.Net.ServicePointManager.SecurityProtocol =
System.Net.SecurityProtocolType.Tls
| System.Net.SecurityProtocolType.Tls11
| System.Net.SecurityProtocolType.Tls12;

var hub = new HubUtil("https://host/hubname");
await hub.StartIfNeededAsync();
hub.Invoke("checkin", "id", "");

关于c# - SignalR 2 StartAsync 永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50508488/

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