Exemplo n.º 1
0
        private static async Task Main(string[] args)
        {
            // 等待 Server 啟動
            await Task.Delay(5000);

            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            // 建立連接到 gRPC 服務的通道
            var channel = GrpcChannel.ForAddress("http://192.168.0.188:5001");



            //var httpClient = new HttpClient(
            //    new HttpClientHandler
            //    {
            //        ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            //    });

            //// 建立連接到 gRPC 服務的通道
            //var channel = GrpcChannel.ForAddress("https://192.168.0.188:5001", new GrpcChannelOptions { HttpClient = httpClient });



            //// 建立連接到 gRPC 服務的通道
            //var channel = GrpcChannel.ForAddress("https://localhost:5001");


            var callInvoker = channel.Intercept(new LogInterceptor());

            // 建立 EmployeeClient
            var client = new Employee.EmployeeClient(callInvoker);

            // 呼叫 GetEmployee()
            var employee = await client.GetEmployeeAsync(new EmployeeRequest { Id = 1 });

            // 輸出 EmployeeModel 的序列化結果
            Console.WriteLine(JsonSerializer.Serialize(employee, new JsonSerializerOptions {
                WriteIndented = true
            }));

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client  = new Greeter.GreeterClient(channel);
            var reply   = client.SayHello(new HelloRequest {
                Name = "GreeterClient"
            });

            Console.WriteLine("Greeting:" + reply.Message);
            string id;

            while ((id = Console.ReadLine()) != "3")
            {
                var empClient = new Employee.EmployeeClient(channel);
                var emp       = empClient.GetEmpInfo(new EmpRequest {
                    Id = Convert.ToInt32(id)
                });

                Console.WriteLine(emp);
            }

            Console.ReadKey();
        }