public override async Task <BasketsResponse> GetBasketItems(Empty request, ServerCallContext context) { var userId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type.Equals("sub"))?.Value; var basketResponse = new BasketsResponse(); if (userId != null) { List <BasketItem> basketItems = null; var basketItemsJson = await _cache.GetStringAsync(userId); if (!string.IsNullOrEmpty(basketItemsJson)) { basketItems = JsonSerializer.Deserialize <List <BasketItem> >(basketItemsJson); foreach (var basketItem in basketItems) { var product = await _productClient.GetProductByIdAsync(new GetProductByIdRequest() { ProductId = basketItem.ProductId }); basketResponse.BasketItems.Add(new BasketItemResponse() { Name = product.Name, Description = product.Description, Price = product.Price, Quantity = basketItem.Quantity, ProductId = product.Id }); } } } return(await Task.FromResult(basketResponse)); }
public async Task <ProductResponse> Get([Required] Guid id) { var response = await GrpcCallerService.CallService(urlGrpc : GRPCUrl.IdentityService, logger : _logger, func : async channel => { var client = new Product.ProductClient(channel); _logger.LogDebug("Grpc get product by id request {@request}", id); return(await client.GetProductByIdAsync(new ProductItemRequest { Id = id.ToString() })); }); return(response); }
static async Task Main(string[] args) { var data = new GetProductByIdModel { ProductId = 2 }; var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Product.ProductClient(grpcChannel); var response = await client.GetProductByIdAsync(data); Console.WriteLine(response); Console.ReadLine(); using (var clientData = client.GetAllProducts(new GetAllProductsRequest())) { while (await clientData.ResponseStream.MoveNext(new System.Threading.CancellationToken())) { var thisProduct = clientData.ResponseStream.Current; Console.WriteLine(thisProduct); } } Console.ReadLine(); }
static async Task Main(string[] args) { //var data = new HelloRequest { Name = "tonmoy" }; //var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001"); //var client = new Greeter.GreeterClient(grpcChannel); //var response = await client.SayHelloAsync(data); //Console.WriteLine(response.Message); //Console.ReadLine(); var data = new GetProductByIdModel { ProductId = 2 }; var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Product.ProductClient(grpcChannel); var response = await client.GetProductByIdAsync(data); Console.WriteLine(response); //var data = new GetStudentByIdModel { StudentId = 2 }; //var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001"); //var client = new Student.StudentClient(grpcChannel); //var response = await client.GetStudentByIdAsync(data); //Console.WriteLine(response); }