如何等待 gRPC 服务器连接?
How to wait for the gRPC server connection?
我正在尝试 运行 Helloworld 示例,客户端使用 C#,服务器使用 Python。
当我手动启动服务器再启动客户端时,客户端可以成功连接到服务器并调用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")
在这一行:
var reply = client.SayHello(new HelloRequest { Name = user });
问题
有什么好的等待连接建立的方法吗?
客户代码(from 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();
}
}
}
您可以使用 CallOptions 中的 "WaitForReady" 选项(默认关闭)等待服务器可用。使用
var reply = client.SayHello(new HelloRequest { Name = user }, new CallOptions().WithWaitForReady(true));
会有想要的效果。
这里介绍了这个选项:
https://github.com/grpc/grpc/pull/8828/files
我正在尝试 运行 Helloworld 示例,客户端使用 C#,服务器使用 Python。
当我手动启动服务器再启动客户端时,客户端可以成功连接到服务器并调用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")
在这一行:
var reply = client.SayHello(new HelloRequest { Name = user });
问题
有什么好的等待连接建立的方法吗?
客户代码(from 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();
}
}
}
您可以使用 CallOptions 中的 "WaitForReady" 选项(默认关闭)等待服务器可用。使用
var reply = client.SayHello(new HelloRequest { Name = user }, new CallOptions().WithWaitForReady(true));
会有想要的效果。
这里介绍了这个选项: https://github.com/grpc/grpc/pull/8828/files