-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrpcClientService.cs
34 lines (30 loc) · 1.12 KB
/
GrpcClientService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Threading.Tasks;
using Grpc.Net.Client;
using CalorieCounterGrpcService;
public class GrpcClientService
{
private readonly CalorieCounter.CalorieCounterClient _client;
public GrpcClientService()
{
var channel = GrpcChannel.ForAddress("https://localhost:5001");
_client = new CalorieCounter.CalorieCounterClient(channel);
}
public async Task<ProductReply> GetProduct(int id)
{
var request = new ProductRequest { Id = id };
var response = await _client.GetProductAsync(request);
Console.WriteLine($"Продукт: {response.Name}, Калорийность: {response.CaloriesPer100G} на 100г");
return response;
}
public async Task AddProduct(string name, float caloriesPer100G)
{
var request = new NewProduct
{
Name = name,
CaloriesPer100G = caloriesPer100G
};
var response = await _client.AddProductAsync(request);
Console.WriteLine($"Добавлен продукт: {response.Name}, Калорийность: {response.CaloriesPer100G} на 100г");
}
}