Example #1
0
        private async static Task CreateProducts(ProductService.ProductServiceClient client)
        {
            var products = new List <Product.Product>
            {
                new Product.Product()
                {
                    Name = "Product X1", Description = "Product X1 Description"
                },
                new Product.Product()
                {
                    Name = "Product X2", Description = "Product X2 Description"
                },
                new Product.Product()
                {
                    Name = "Product X3", Description = "Product X3 Description"
                },
                new Product.Product()
                {
                    Name = "Product X4", Description = "Product X4 Description"
                }
            };

            using (var call = client.CreateProducts())
            {
                var requestStream  = call.RequestStream;
                var responseStream = call.ResponseStream;

                var responseTask = Task.Run(async() =>
                {
                    while (await responseStream.MoveNext())
                    {
                        Console.WriteLine("Saved: " + responseStream.Current.Product);
                    }
                });

                foreach (var product in products)
                {
                    await requestStream.WriteAsync(new CreateProductRequest()
                    {
                        Product = product
                    });

                    System.Threading.Thread.Sleep(1000);
                }
                await call.RequestStream.CompleteAsync();

                await responseTask;
            }
        }