private static async Task ProcessMesages(ImageSvc.ImageSvcClient client) { //var cts = new CancellationTokenSource(); //cts.CancelAfter(TimeSpan.FromSeconds(10)); //using var call = client.Download(cancellationToken: cts.Token); using var call = client.Download(); int counter = 0; var readTask = Task.Run(async() => { await foreach (var message in call.ResponseStream.ReadAllAsync()) { Console.WriteLine($"Received content for {message.Url}. Length: {message.Content.Length}"); Interlocked.Increment(ref counter); Directory.CreateDirectory("Images"); File.WriteAllBytes(Path.Combine("Images", $"img{counter}.gif"), message.Content.ToByteArray()); } }); foreach (var url in urls) { await call.RequestStream.WriteAsync(new ImageRequest() { Url = url }).ConfigureAwait(false); } await call.RequestStream.CompleteAsync().ConfigureAwait(false); await readTask.ConfigureAwait(false); }
static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new ImageSvc.ImageSvcClient(channel); Console.WriteLine("Press any key to start downloading images"); Console.ReadKey(); await ProcessMesages(client).ConfigureAwait(false); Console.WriteLine("Shutting down"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }