private async Task <ShoppingCartModel> GetOrCreateShoppingCartAsync(ShoppingCartProtoService.ShoppingCartProtoServiceClient scClient, string token) { ShoppingCartModel shoppingCartModel; try { _logger.LogInformation("GetShoppingCartAsync started.."); var headers = new Metadata(); headers.Add("Authorization", $"Bearer {token}"); shoppingCartModel = await scClient.GetShoppingCartAsync(new GetShoppingCartRequest { Username = _config.GetValue <string>("WorkerService:UserName") }, headers); _logger.LogInformation("GetShoppingCartAsync Response: {shoppingCartModel}", shoppingCartModel); } catch (RpcException exception) { if (exception.StatusCode == StatusCode.NotFound) { _logger.LogInformation("CreateShoppingCartAsync started.."); shoppingCartModel = await scClient.CreateShoppingCartAsync(new ShoppingCartModel { Username = _config.GetValue <string>("WorkerService:UserName") }); _logger.LogInformation("CreateShoppingCartAsync Response: {shoppingCartModel}", shoppingCartModel); } else { throw; } } return(shoppingCartModel); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); //0 Get Token from IS4 //1 Create SC if not exist //2 Retrieve products from product grpc with server stream //3 Add sc items into SC with client stream //0 Get Token from IS4 var token = await GetTokenFromIS4(); //1 Create SC if not exist using var scChannel = GrpcChannel.ForAddress(_config.GetValue <string>("WorkerService:ShoppingCartServerUrl")); var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel); var scModel = await GetOrCreateShoppingCartAsync(scClient, token); // open sc client stream using var scClientStream = scClient.AddItemIntoShoppingCart(); //2 Retrieve products from product grpc with server stream using var productChannel = GrpcChannel.ForAddress(_config.GetValue <string>("WorkerService:ProductServerUrl")); var productClient = new ProductProtoService.ProductProtoServiceClient(productChannel); _logger.LogInformation("GetAllProducts started.."); using var clientData = productClient.GetAllProducts(new GetAllProductsRequest()); await foreach (var responseData in clientData.ResponseStream.ReadAllAsync()) { _logger.LogInformation("GetAllProducts Stream Response: {responseData}", responseData); //3 Add sc items into SC with client stream var addNewScItem = new AddItemIntoShoppingCartRequest { Username = _config.GetValue <string>("WorkerService:UserName"), DiscountCode = "CODE_100", NewCartItem = new ShoppingCartItemModel { ProductId = responseData.ProductId, Productname = responseData.Name, Price = responseData.Price, Color = "Black", Quantity = 1 } }; await scClientStream.RequestStream.WriteAsync(addNewScItem); _logger.LogInformation("ShoppingCart Client Stream Added New Item : {addNewScItem}", addNewScItem); } await scClientStream.RequestStream.CompleteAsync(); var addItemIntoShoppingCartResponse = await scClientStream; _logger.LogInformation("AddItemIntoShoppingCart Client Stream Response: {addItemIntoShoppingCartResponse}", addItemIntoShoppingCartResponse); await Task.Delay(_config.GetValue <int>("WorkerService:TaskInterval"), stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Console.WriteLine("Waiting for server is running..."); Thread.Sleep(2000); while (!stoppingToken.IsCancellationRequested) { var interval = _configuration.GetValue <int>("WorkerService:TaskInterval"); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); var scUrl = _configuration.GetValue <string>("WorkerService:ShoppingCartServerUrl"); using var scChannel = GrpcChannel.ForAddress(scUrl); var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel); var token = await GetTokenFromIS4(); var scModel = await GetOrCreateShoppingCartAsync(scClient, token); var pcUrl = _configuration.GetValue <string>("WorkerService:ProductServerUrl"); using var scClientStream = scClient.AddItemIntoShoppingCart(); var pChannel = GrpcChannel.ForAddress(pcUrl); var productClient = new ProductProtoService.ProductProtoServiceClient(pChannel); _logger.LogInformation("GetAllProducts started..."); using var clietData = productClient.GetAllProducts(new GetAllProductsRequest()); await foreach (var responseData in clietData.ResponseStream.ReadAllAsync()) { _logger.LogInformation($"GetAllProducts Stream Response: {responseData}", responseData); var userName = _configuration.GetValue <string>("WorkerService:UserName"); var addNewScItem = new AddItemIntoShoppingCartRequest { Username = userName, DiscountCode = "CODE_100", NewCartItem = new ShoppingCartItemModel { ProductId = responseData.Id, ProductName = responseData.Name, Price = responseData.Price, Color = "Black", Quantity = 1 } }; await scClientStream.RequestStream.WriteAsync(addNewScItem); _logger.LogInformation($"ShoppingCart Client Stream Added new Item : {addNewScItem}", addNewScItem); } await scClientStream.RequestStream.CompleteAsync(); var addItemIntoShoppingCartResponse = await scClientStream; _logger.LogInformation($"AddItemIntoShoppingCart Client Stream Response : {addItemIntoShoppingCartResponse}", addItemIntoShoppingCartResponse); await Task.Delay(interval, stoppingToken); } }
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"); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Thread.Sleep(3000); while (!stoppingToken.IsCancellationRequested) { //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); using var scChannel = GrpcChannel.ForAddress(_configuration.GetValue <string>("WorkerService:ShoppingCartServerURL")); var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel); //Get token from identity Server var token = await GetTokenFromIdentityServer(); var scModel = await GetOrCreateShoppingModel(scClient, token); using var scClientStraem = scClient.AddItemIntoShopppingCart(); // get Products using var productCannel = GrpcChannel.ForAddress(_configuration.GetValue <string>("WorkerService:ProductServerURL")); var productClient = new ProductProtoService.ProductProtoServiceClient(productCannel); _logger.LogInformation("*****GetAllProducts Started****"); using var clientData = productClient.GetAllProducts(new GetAllProductRequest()); await foreach (var product in clientData.ResponseStream.ReadAllAsync()) { _logger.LogInformation("--Each Product--" + product); var newSCItem = new AddItemIntoShopppingCartRequest { Username = _configuration.GetValue <string>("WorkerService:UserName"), DiscountCode = "D100", NewCartItem = new ShoppingCartItemModel { ProductId = product.ProductId, Color = "GREEN", Price = product.Price, Productname = product.Name, Quantity = 1 } }; await scClientStraem.RequestStream.WriteAsync(newSCItem); _logger.LogInformation("Written Client Stream" + newSCItem.ToString()); } await scClientStraem.RequestStream.CompleteAsync(); var addItemClientStreamResponse = await scClientStraem; _logger.LogInformation("Add Client Stream Retsponse" + addItemClientStreamResponse.ToString()); await Task.Delay(_configuration.GetValue <int>("WorkerService:TaskInterval"), stoppingToken); } }