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

时间:2020-10-29 12:00:09

标签: c# .net grpc

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

                    var request = new InvoiceSubscription { };

                    using var call = lightningClient.SubscribeInvoices(request, null, 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, null, null, 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
                    }

0 个答案:

没有答案