7
votes

I am trying to run the Helloworld example with the client in C# and the server in Python.

When I manually start the server and then the client, the client can successfully connect to the server and call the SayHello method.

Now, I have configured my IDE (Visual Studio) to start both the client and the server at the same time. The client fails with an RpcException thrown:

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

at this line:

var reply = client.SayHello(new HelloRequest { Name = user });

Question

Is there a good way to wait for the connection to be established?

Client Code (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();
        }
    }
}
1

1 Answers

10
votes

You can use the "WaitForReady" option from the CallOptions (it's off by default) to wait for the server to become available. Using

var reply = client.SayHello(new HelloRequest { Name = user }, new CallOptions().WithWaitForReady(true));

will have the desired effect.

The option was introduced here: https://github.com/grpc/grpc/pull/8828/files