如何等待gRPC服务器连接?

joh*_*mik 1 c# grpc

我正在尝试使用C#中的客户端和Python中的服务器来运行Helloworld示例.

当我手动启动服务器然后手动启动客户端时,客户端可以成功连接到服务器并调用该SayHello方法.

现在,我已经配置了我的IDE(Visual Studio)来同时启动客户端和服务器.客户端因RpcException抛出而失败:

An unhandled exception of type 'Grpc.Core.RpcException' occurred in mscorlib.dll
Additional information: Status(StatusCode=Unavailable, Detail="Connect Failed")
Run Code Online (Sandbox Code Playgroud)

在这一行:

var reply = client.SayHello(new HelloRequest { Name = user });
Run Code Online (Sandbox Code Playgroud)

有没有一种好方法可以等待连接建立?

客户端代码(来自grpc/examples)

using System;
using Grpc.Core;
using Helloworld;

namespace CSharpClient
{
    class Program
    {
        public static void Main(string[] args)
        {
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

            var client = new Greeter.GreeterClient(channel);

            String user = "you";

            var reply = client.SayHello(new HelloRequest { Name = user });
            Console.WriteLine("Greeting: " + reply.Message);

            channel.ShutdownAsync().Wait();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*sch 6

您可以使用CallOptions中的"WaitForReady"选项(默认情况下已关闭)等待服务器可用.运用

var reply = client.SayHello(new HelloRequest { Name = user }, new CallOptions().WithWaitForReady(true));
Run Code Online (Sandbox Code Playgroud)

会产生预期的效果.

这里介绍了这个选项:https: //github.com/grpc/grpc/pull/8828/files