예제 #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                int delay = _config.GetValue <int>("WorkerService:TaskInterval");
                await Task.Delay(delay, stoppingToken);

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                // Get Token

                var token = await GetTokenFromIS();

                //create Sopping cart
                //Retrieve products from server
                //add products to a sc
                using var scChannel = GrpcChannel
                                      .ForAddress(_config.GetValue <string>("WorkerService:ShoppingCartServerUrl"));

                //get SC
                var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel);
                var scModel  = await GetOrCreateShoppingCartAsync(scClient, token);

                //open shopping cart stream
                using var scClientStream = scClient.AddItemIntoShoppingCart();

                //retrive products
                using var productChannel = GrpcChannel
                                           .ForAddress(_config.GetValue <string>("WorkerService:ProductServerUrl"));
                var productClient = new ProductsProtoService.ProductsProtoServiceClient(productChannel);

                _logger.LogInformation("GetAllProducts --->");
                using var _clientData = productClient.GetAllProducts(new GetAllProductRequest());
                await foreach (var responseData in _clientData.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine("currentProduct - " + responseData.ToString());
                    var addNewScItem = new AddItemIntoShoppingCartRequest
                    {
                        Username     = _config.GetValue <string>("WorkerService:UserName"),
                        DiscountCode = "CODE_90",
                        NewCartItem  = new ShoppingCartItemModel
                        {
                            Color       = "red",
                            Price       = responseData.Price,
                            ProductId   = responseData.ProductId,
                            ProductName = responseData.Name,
                            Quantity    = 1
                        }
                    };

                    await scClientStream.RequestStream.WriteAsync(addNewScItem);
                }
                await scClientStream.RequestStream.CompleteAsync();

                var addItemIntoSCResponce = await scClientStream;
                _logger.LogInformation($"Added  {addItemIntoSCResponce.InsertCount} products to a shopping cart");
            }
        }
예제 #2
0
        private static async Task GetAllProductsAsync(ProductsProtoService.ProductsProtoServiceClient client)
        {
            Console.WriteLine("GetAllProducts started..");

            using var _clientData = client.GetAllProducts(new GetAllProductRequest());
            await foreach (var responseData in _clientData.ResponseStream.ReadAllAsync())
            {
                Console.WriteLine("currentProduct - " + responseData.ToString());
            }
        }