简体   繁体   English

SignalR,如何处理 Startasync 失败

[英]SignalR , how to handle Startasync failing

I'm encountering some problems with a signalR hub , i need to invoke a certain method from a signalR hub whenever i receive a request from an external service .我在使用 signalR 集线器时遇到了一些问题,每当我收到来自外部服务的请求时,我都需要从 signalR 集线器调用某个方法。 Whenever i do receive such "request" i connect to the signalRhub and then invoke one of its methods.每当我收到这样的“请求”时,我都会连接到 signalRhub,然后调用它的一种方法。

 string hostname = Environment.GetEnvironmentVariable("FRONTEND_HOSTNAME");
                    HubConnection connection;

                    connection = new HubConnectionBuilder()
                    .WithUrl($"{hostname}/xxx")
                    .Build();

                    await connection.StartAsync();
                    var resp = new
                    {
                        xx:'xx',
                    };
                    await connection.InvokeAsync("SendReport", resp);

                    connection.Closed += async (error) =>
                    {
                        await Task.Delay(new Random().Next(0, 5) * 1000);
                        await connection.StartAsync();
                    };

The whole implementation has already been done , although it seems that i'm able to connect & send messages ALMOST always without problems, however from time to time the StartAsync() method fails and throws an exception "Name or service not known" , to me it looks like it's unable to connect due to network issues , if you had to implement a way to retry the StartAsync a few times to then invoke the hub method, how would you do it?整个实现已经完成,虽然我似乎能够连接和发送消息几乎总是没有问题,但是 StartAsync() 方法有时会失败并抛出异常“名称或服务未知”,以我看起来由于网络问题而无法连接,如果您必须实现一种方法来重试 StartAsync 几次然后调用集线器方法,您会怎么做?

I'm not asking for a straight solution , an example of something / links to documentation would still be great, i did search for a way to handle this but with no luck我不是要一个直接的解决方案,一些东西的例子/文档链接仍然很好,我确实在寻找一种方法来处理这个问题但没有运气

Sorry it appears i didn't look properly within the documentation , here's the answer i was looking for:抱歉,我似乎没有在文档中正确查看,这是我正在寻找的答案:

public static async Task<bool> ConnectWithRetryAsync(HubConnection connection, CancellationToken token){
// Keep trying to until we can start or the token is canceled.
while (true)
{
    try
    {
        await connection.StartAsync(token);
        Debug.Assert(connection.State == HubConnectionState.Connected);
        return true;
    }
    catch when (token.IsCancellationRequested)
    {
        return false;
    }
    catch
    {
        // Failed to connect, trying again in 5000 ms.
        Debug.Assert(connection.State == HubConnectionState.Disconnected);
        await Task.Delay(5000);
    }
}}

https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-6.0&tabs=visual-studio https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-6.0&tabs=visual-studio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM