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); } }
private static async Task GetAllProducts(ProductProtoService.ProductProtoServiceClient client) { Console.WriteLine("GetAllProducts started..."); using var clientData = client.GetAllProducts(new GetAllProductsRequest()); await foreach (var responseData in clientData.ResponseStream.ReadAllAsync()) { Console.WriteLine(responseData); } }
private static async Task GetAllProductAsync(ProductProtoService.ProductProtoServiceClient client) { //Get All Product using C# 9 using var clientData = client.GetAllProducts(new GetAllProductRequest()); await foreach (var product in clientData.ResponseStream.ReadAllAsync()) { Console.WriteLine(product); } }
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); } }