[英]How to wait for the gRPC server connection?
我正在尝试使用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")
在这一行:
var reply = client.SayHello(new HelloRequest { Name = user });
有没有好的方法等待连接建立?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.