Exemplo n.º 1
0
        static async Task Main()
        {
            // This switch must be set before creating the GrpcChannel/HttpClient.
            AppContext.SetSwitch(
                "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            // using (var channel = GrpcChannel.ForAddress("http://*****:*****@"{countryData.CountryFull}
                                                                            {countryData.Message}
                                                                            with {countryData.Players.Count} registered players");
                    }
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
                {
                    Console.WriteLine("Stream cancelled.");
                }

                var aCountry = await client.GetCountryAsync(new CountryRequest { Country = "Italy" });

                Console.WriteLine($"Seeking Italy; Found {aCountry.ToString()}");

                aCountry = await client.GetCountryAsync(new CountryRequest { Country = "No Country" });

                Console.WriteLine($"Seeking non existent country; Found {aCountry.ToString()}");

                #endregion


                #region Consume Players
                var playersClient = new PlayerDirectoryClient(channel);
                var playerCts     = new CancellationTokenSource(TimeSpan.FromSeconds(10));
                using (var playersStreamingCall = playersClient.GetPlayers(new Empty(), cancellationToken: playerCts.Token))
                {
                    try
                    {
                        await foreach (PlayersReply playerData in playersStreamingCall.ResponseStream.ReadAllAsync())
                        {
                            Console.WriteLine($@"{playerData.Message}
                            {playerData.Players.ToString()}");
                        }
                    }
                    catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
                    {
                        Console.WriteLine("Stream cancelled.");
                    }
                }

                PlayerReply aPlayer = await playersClient.GetPlayerAsync(new PlayerRequest { Name = "Hasan Sukur", Country = "Turkey" });

                Console.WriteLine($"Should have found: {aPlayer.ToString()}");


                PlayerReply notFound = await playersClient.GetPlayerAsync(new PlayerRequest { Name = "Idiakose O. Sunday" });

                Console.WriteLine($"Should have found me. Hehehehhe: {notFound.ToString()}");

                #endregion
            }
        }