Ejemplo n.º 1
0
        private static async Task BeStreaming()
        {
            using (var call = _client.BeStreaming())
            {
                var responseReaderTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        var note = call.ResponseStream.Current;
                        Console.WriteLine(note.Message);
                    }
                });

                IEnumerable <StreamRequest> requests = new List <StreamRequest>
                {
                    new StreamRequest
                    {
                        Message = "First message",
                        Count   = 1
                    },
                    new StreamRequest
                    {
                        Message = "Second message",
                        Count   = 2
                    },
                    new StreamRequest
                    {
                        Message = "Third message",
                        Count   = 3
                    }
                };
                foreach (var request in requests)
                {
                    await Task.Delay(1000);

                    Console.WriteLine("Sending request");
                    await call.RequestStream.WriteAsync(request);
                }

                await call.RequestStream.CompleteAsync();

                await responseReaderTask;
            }
        }