private static async Task callClientStreaming(Greeter.GreeterClient client) { using var clientStreamingCall = client.SayHelloClientStreaming(); for (int i = 0; i < 5; i++) { string name = $"John #{i}"; Console.WriteLine($"Sending name: {name}"); try { await clientStreamingCall.RequestStream.WriteAsync(new HelloRequest { Name = name }); } catch (InvalidOperationException ex) { Console.WriteLine($"InvalidOperationException: {ex.Message}"); // Can't write the message because the call is complete. return; } await Task.Delay(500); } await clientStreamingCall.RequestStream.CompleteAsync(); HelloReply reply = await clientStreamingCall; reply.WriteToConsole(); }
public static async Task Main(string[] args) { var client = createClient(); HelloRequest request = new HelloRequest { Name = "John Doe" }; // Call: SayHello HelloReply reply = await client.SayHelloAsync(request); reply.WriteToConsole(); // Call: ThrowRpcException try { await client.ThrowRpcExceptionAsync(new Empty()); } catch (RpcException ex) { Console.WriteLine(ex.Status); } // Call: ServerStreaming await callServerStreaming(client, request); // Call: ClientStreaming await callClientStreaming(client); // Call: SayHelloCertAuth reply = await client.SayHelloCertAuthAsync(request); // RpcException: Status(StatusCode=PermissionDenied, Detail="Bad gRPC response. HTTP status code: 403") reply.WriteToConsole(); }