예제 #1
0
        private static void HandleUnaryCall(TestService.TestServiceClient client)
        {
            var inputKey = Console.ReadLine();
            var input    = new EchoInput()
            {
                Input = inputKey
            };
            var tokensource = new CancellationTokenSource();

            if (inputKey == "Q")
            {
                tokensource.Cancel();
            }

            try
            {
                var res = client.GetEcho(input, cancellationToken: tokensource.Token);
                Console.WriteLine("Get echo: " + res.Output.Trim());
            }
            catch (RpcException e) when(e.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Operation cancelled.");
            }
            finally
            {
                tokensource.Dispose();
            }
        }
예제 #2
0
        /// <summary>
        /// Get echo of input, unary call
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task <EchoOutput> GetEcho(EchoInput request, ServerCallContext context)
        {
            _logger.LogTrace($"{context.Host} {context.Method}");

            var output = new StringBuilder();

            output.AppendLine("Your input is: " + request.Input);
            output.AppendLine("The server is: " + Environment.MachineName);

            var returnVal = new EchoOutput()
            {
                Output    = output.ToString(),
                TimeStamp = Timestamp.FromDateTime(DateTime.UtcNow)
            };

            return(Task.FromResult(returnVal));
        }
예제 #3
0
        /// <summary>
        /// Get echo returned as stream, server streaming call
        /// </summary>
        /// <param name="request"></param>
        /// <param name="responseStream"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task GetEchoStream(EchoInput request, IServerStreamWriter <EchoOutput> responseStream, ServerCallContext context)
        {
            _logger.LogTrace($"{context.Host} {context.Method}");

            for (int i = 0; i < 3; i++)
            {
                var returnVal = new EchoOutput()
                {
                    Output    = request.Input.ToString(),
                    TimeStamp = Timestamp.FromDateTime(DateTime.UtcNow)
                };

                _logger.LogInformation("Looping echo stream count " + i);

                await responseStream.WriteAsync(returnVal);

                //Sleep 3s
                Thread.Sleep(3000);
            }
        }
예제 #4
0
        public static async Task HandleServerStreamingCallAsync(TestService.TestServiceClient client)
        {
            var inputKey = Console.ReadLine();
            var input    = new EchoInput()
            {
                Input = inputKey
            };

            var tokenSource = new CancellationTokenSource();

            if (inputKey == "Q")
            {
                tokenSource.Cancel();
            }

            var resStream = client.GetEchoStream(input, deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: tokenSource.Token);

            while (await resStream.ResponseStream.MoveNext())
            {
                Console.WriteLine("Get echo from stream: " + resStream.ResponseStream.Current.Output.Trim());
                Console.WriteLine("Get echo time: " + resStream.ResponseStream.Current.TimeStamp.ToDateTime().ToLocalTime());
            }
        }