-
Notifications
You must be signed in to change notification settings - Fork 77
/
Program.cs
46 lines (42 loc) · 1.35 KB
/
Program.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
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using Grpc.Core;
using Hello;
namespace HelloClient
{
class Program
{
public static void Main(string[] args)
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new HelloService.HelloServiceClient(channel);
// ideally you should check for errors here too
var reply = client.SayHello(new HelloReq { Name = "Euler" });
Console.WriteLine(reply.Result);
try
{
reply = client.SayHelloStrict(new HelloReq { Name = "Leonhard Euler" });
Console.WriteLine(reply.Result);
}
catch (RpcException e)
{
// ouch!
// lets print the gRPC error message
// which is "Length of `Name` cannot be more than 10 characters"
Console.WriteLine(e.Status.Detail);
// lets access the error code, which is `INVALID_ARGUMENT`
Console.WriteLine(e.Status.StatusCode);
// Want its int version for some reason?
// you shouldn't actually do this, but if you need for debugging,
// you can access `e.Status.StatusCode` which will give you `3`
Console.WriteLine((int)e.Status.StatusCode);
// Want to take specific action based on specific error?
if (e.Status.StatusCode == Grpc.Core.StatusCode.InvalidArgument) {
// do your thing
}
}
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}