如何在.NET gRPC流客户端中检查连接

问题描述

我在.NET中有一个gRPC服务器流客户端,如下所示:

                    var request = new InvoiceSubscription { };

                    using var call = lightningClient.SubscribeInvoices(request,null,cancellationToken);
                    
                    // If the connection to the lightningClient is broken the line below throws
                    while (await call.ResponseStream.MoveNext(cancellationToken))
                    {
                        var invoice = call.ResponseStream.Current;

                        // handle the newly received invoice...
                    }

现在,如果gRPC服务器已关闭/没有连接await call.ResponseStream.MoveNext(cancellationToken)抛出。

有没有一种很好的方法来检查gRPC服务器的连接是否存在而又不会抛出异常?

我正在寻找类似的东西,但这不起作用:

                    using var call = lightningClient.SubscribeInvoices(request,cancellationToken);

                    // get status from streaming call
                    var status = call.GetStatus();

                    // determine if status code OK is returned
                    if (status.StatusCode == StatusCode.OK)
                    {
                        // Ok,handle connection alive
                    }
                    else
                    {
                        // Ok,handle connection not alive
                    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)