Example #1
0
        static async Task Main(string[] args)
        {
            // Server will only support Https on Windows and Linux
            var credentials = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ChannelCredentials.Insecure : ClientResources.SslCredentials;
            var channel     = new Channel("localhost:50051", credentials);
            var client      = new Counter.CounterClient(channel);

            var reply = client.IncrementCount(new Google.Protobuf.WellKnownTypes.Empty());

            Console.WriteLine("Count: " + reply.Count);

            using (var call = client.AccumulateCount())
            {
                for (int i = 0; i < 3; i++)
                {
                    var count = RNG.Next(5);
                    Console.WriteLine($"Accumulating with {count}");
                    await call.RequestStream.WriteAsync(new CounterRequest { Count = count });

                    await Task.Delay(1000);
                }

                await call.RequestStream.CompleteAsync();

                Console.WriteLine($"Count: {(await call.ResponseAsync).Count}");
            }

            Console.WriteLine("Shutting down");
            await channel.ShutdownAsync();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Example #2
0
        static async Task Main(string[] args)
        {
            var channel = new Channel("localhost:50051", ClientResources.SslCredentials);
            var client  = new Counter.CounterClient(channel);

            var reply = client.IncrementCount(new Google.Protobuf.WellKnownTypes.Empty());

            Console.WriteLine("Count: " + reply.Count);

            using (var call = client.AccumulateCount())
            {
                for (int i = 0; i < 3; i++)
                {
                    var count = RNG.Next(5);
                    Console.WriteLine($"Accumulating with {count}");
                    await call.RequestStream.WriteAsync(new CounterRequest { Count = count });

                    await Task.Delay(1000);
                }

                await call.RequestStream.CompleteAsync();

                Console.WriteLine($"Count: {(await call.ResponseAsync).Count}");
            }

            Console.WriteLine("Shutting down");
            channel.ShutdownAsync().Wait();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Example #3
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Staring client streaming call at :{time}", DateTimeOffset.Now);
            _clientStreamingCall = _counterClient.AccumulateCount();

            await base.StartAsync(cancellationToken);
        }
Example #4
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting client streaming call at: {time}", DateTimeOffset.Now);
            // Don't pass cancellation token to the call. The call is completed in StopAsync when service stops.
            _clientStreamingCall = _counterClient.AccumulateCount(cancellationToken: CancellationToken.None);

            await base.StartAsync(cancellationToken);
        }
        public override async Task <CounterReply> AccumulateCount(IAsyncStreamReader <CounterRequest> requestStream, ServerCallContext context)
        {
            using (var call = _counterClient.AccumulateCount())
            {
                await foreach (var message in requestStream.ReadAllAsync())
                {
                    await call.RequestStream.WriteAsync(message);
                }

                await call.RequestStream.CompleteAsync();

                return(await call);
            }
        }
Example #6
0
        public override async Task <CounterReply> AccumulateCount(IAsyncStreamReader <CounterRequest> requestStream, ServerCallContext context)
        {
            // Forward the call on to the counter service
            using (var call = _counterClient.AccumulateCount())
            {
                while (await requestStream.MoveNext(CancellationToken.None))
                {
                    await call.RequestStream.WriteAsync(requestStream.Current);
                }

                await call.RequestStream.CompleteAsync();

                return(await call);
            }
        }
        private static async Task ClientStreamingCallExample(Counter.CounterClient client)
        {
            using var call = client.AccumulateCount();
            for (var i = 0; i < 3; i++)
            {
                var count = Random.Next(5);
                Console.WriteLine($"Accumulating with {count}");
                await call.RequestStream.WriteAsync(new CounterRequest { Count = count });

                await Task.Delay(TimeSpan.FromSeconds(2));
            }

            await call.RequestStream.CompleteAsync();

            var response = await call;

            Console.WriteLine($"Count: {response.Count}");
        }
Example #8
0
        /// <summary>
        /// 客户端流式调用
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        private static async Task ClientStreamingCallExample(Counter.CounterClient client)
        {
            using (var call = client.AccumulateCount())
            {
                for (int i = 0; i < 3; i++)
                {
                    var count = RNG.Next(5);
                    Console.WriteLine($"Accumulating with {count}");
                    await call.RequestStream.WriteAsync(new CounterRequest { Count = count });

                    await Task.Delay(2000);
                }
                await call.RequestStream.CompleteAsync();

                var response = await call;
                Console.WriteLine($"Count:{response.Count}");
            }
        }