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
 public AggregatorService(
     Greeter.GreeterClient greeterClient,
     Counter.CounterClient counterClient)
 {
     _greeterClient = greeterClient;
     _counterClient = counterClient;
 }
Example #3
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();
        }
        private static async Task ServerStreamingCallExample(Counter.CounterClient client)
        {
            using var call = client.Countdown(new Empty());

            await foreach (var message in call.ResponseStream.ReadAllAsync())
            {
                Console.WriteLine($"Countdown: {message.Count}");
            }
        }
Example #5
0
        public AuthenticatedCounterClient(GrpcChannel channel, IScopeContext <IAuthenticatedCounterClient> context, IBearerTokenProvider tokenProvider)
        {
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }

            _context       = context ?? throw new ArgumentNullException(nameof(context));
            _tokenProvider = tokenProvider ?? throw new ArgumentNullException(nameof(tokenProvider));

            _client = new Counter.CounterClient(channel);
        }
Example #6
0
        static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client  = new Counter.CounterClient(channel);

            await UnaryCallExample(client);

            await ClientStreamingCallExample(client);

            Console.WriteLine("Shutting down");
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Example #7
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);

            await UnaryCallExample(client);

            await ClientStreamingCallExample(client);

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

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        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 #9
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}");
            }
        }
Example #10
0
        private static async Task RunCounterExample(GrpcChannel channel)
        {
            var client = new Counter.CounterClient(channel);

            do
            {
                Console.Write("Press Enter");
                try
                {
                    var response = await client.GetCountAsync(new Google.Protobuf.WellKnownTypes.Empty());

                    Console.WriteLine($"Count: {response.Count}");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            } while (Console.ReadKey().Key == ConsoleKey.Enter);
        }
Example #11
0
        static async Task Main(string[] args)
        {
            // 为客户端配置选项
            // 更多配置见:https://docs.microsoft.com/zh-cn/aspnet/core/grpc/configuration?view=aspnetcore-3.0#configure-client-options
            var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
            {
                MaxReceiveMessageSize = 5 * 1024 * 1024, // 5MB
                MaxSendMessageSize    = 2 * 1024 * 1024  // 2MB
            });


            var clientGreeter = new Greeter.GreeterClient(channel);
            var clientCounter = new Counter.CounterClient(channel);
            //var clientRacer = new Racer.RacerClient(channel);
            var clientAggregator = new Aggregator.AggregatorClient(channel);

            //await UnaryStreamCallExample(clientGreeter);
            //await ServerStreamingCallExample(clientGreeter);
            //await ClientStreamingCallExample(clientCounter);
            //await BidirectionalStreamingExample(clientRacer);
            await AggregatorServerStreamingCallExample(clientAggregator);

            Console.ReadLine();
        }
Example #12
0
        private static async Task UnaryCallExample(Counter.CounterClient client)
        {
            var reply = await client.IncrementCountAsync(new Google.Protobuf.WellKnownTypes.Empty());

            Console.WriteLine("Count: " + reply.Count);
        }
Example #13
0
 public Worker(ILogger <Worker> logger, Counter.CounterClient client)
 {
     _logger        = logger;
     _counterClient = client;
     _random        = new Random();
 }
        private static async Task UnaryCallExample(Counter.CounterClient client)
        {
            var reply = await client.IncrementCountAsync(new Empty());

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