示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Grpc!");

            using var channel = GrpcChannel.ForAddress(@"https://chyagrpc.northeurope.azurecontainer.io:5001",
                                                       new GrpcChannelOptions()
            {
                MaxReceiveMessageSize = null,
                MaxSendMessageSize    = null
            });
            var client = new TestService.TestServiceClient(channel);

            var sysInfo = client.GetSysInfo(new Google.Protobuf.WellKnownTypes.Empty());

            Console.WriteLine(sysInfo.Output);

            var ts = client.GetDateTime(new Google.Protobuf.WellKnownTypes.Empty());

            Console.WriteLine("Google Protobuf Time: " + ts.TimeStamp.ToDateTime().ToLocalTime());

            Console.WriteLine("Type in something to echo (Unary call, type Q to cancel): ");
            HandleUnaryCall(client);

            Console.WriteLine("Type in something to get echo stream (Server streaming call, type Q to cancel): ");
            var task = HandleServerStreamingCallAsync(client);

            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var ie in ex.InnerExceptions)
                {
                    if (ie is RpcException)
                    {
                        switch (((RpcException)ie).StatusCode)
                        {
                        case StatusCode.DeadlineExceeded:
                            Console.WriteLine("Exceed deadline.");
                            break;

                        case StatusCode.Cancelled:
                            Console.WriteLine("Operation cancelled.");
                            break;

                        default:
                            break;
                        }
                    }
                }
            }

            Console.WriteLine("Type 3 line to get a echo return (Client streaming call, type Q to cancel): ");
            HandleClientStreamingCall(client);

            Console.WriteLine("Type 3 line to get echo return in stream (Client/Server streaming call, type Q to cancel): ");
            var task2 = HandleClientServerStreamingCall(client);

            try
            {
                task2.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var ie in ex.InnerExceptions)
                {
                    if (ie is RpcException)
                    {
                        switch (((RpcException)ie).StatusCode)
                        {
                        case StatusCode.DeadlineExceeded:
                            Console.WriteLine("Exceed deadline.");
                            break;

                        case StatusCode.Cancelled:
                            Console.WriteLine("Operation cancelled.");
                            break;

                        default:
                            break;
                        }
                    }
                }
            }

            Console.WriteLine("Type any key to close demo...");
            Console.ReadKey();
        }