I'm rather new to C#, and I need to use GRPC over TLS.
As a dry-run, I'm modifying the example provided in the main grpc repo to use TLS. To do this, I found another SO question with what seemed like a good answer: How to enable server side SSL for gRPC?. However, I get an error Unhandled Exception: Grpc.Core.RpcException: Status(StatusCode=Unavailable, Detail="Connect Failed")
(full trace below).
I get the same error if I in the original, non-tls code, specify an incorrect port or just don't start the server. I'm using dotnet core on Ubuntu.
The important parts of the code are below, and also found in full on a fork on github.
Client:
var cacert = File.ReadAllText(@"../ca.crt"); var clientcert = File.ReadAllText(@"../client.crt"); var clientkey = File.ReadAllText(@"../client.key"); var ssl = new SslCredentials(cacert, new KeyCertificatePair(clientcert, clientkey)); var channel = new Channel("localhost", 555, ssl); 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();
Server:
var cacert = File.ReadAllText(@"../ca.crt"); var servercert = File.ReadAllText(@"../server.crt"); var serverkey = File.ReadAllText(@"../server.key"); var keypair = new KeyCertificatePair(servercert, serverkey); var sslCredentials = new SslServerCredentials(new List<KeyCertificatePair>() {keypair}, cacert, false); var server = new Server { Services = {Greeter.BindService(new GreeterImpl())}, Ports = {new ServerPort("0.0.0.0", 555, sslCredentials)} }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait();
Full output of the programs:
$ cd GreeterClient $ dotnet run -f netcoreapp1.0 Unhandled Exception: Grpc.Core.RpcException: Status(StatusCode=Unavailable, Detail="Connect Failed") at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg) at Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) at Grpc.Core.Internal.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) at Helloworld.Greeter.GreeterClient.SayHello(HelloRequest request, CallOptions options) at Helloworld.Greeter.GreeterClient.SayHello(HelloRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) at GreeterClient.Program.Main(String[] args)
And server:
$ cd GreeterServer/ $ dotnet run -f netcoreapp1.0 Greeter server listening on port 50051 Press any key to stop the server...
Did I do some silly mistake, or does this not run on non-windows machines? Is there some way to debug the issue to figure out what is going on?