Beispiel #1
0
        private const string ServerAdderss = "https://localhost:5001";  //服务端的地址
        protected void GetEmployeeById()
        {
            Response1 = string.Empty;   //清空前台显示
            var metaData = new Metadata //元数据都是一些 key-value对
            {
                { "myKey", "myValue" }  //随便假装一点 key-value对
            };

            if (int.TryParse(Request1, out var id))
            {
                //*****************************主要是这里********************************
                using var channel = GrpcChannel.ForAddress(ServerAdderss,
                                                           new GrpcChannelOptions
                {
                    HttpClient = _httpClient
                });              //创建通道
                var client = new EmployeeService.EmployeeServiceClient(channel);
                try
                {
                    var response = client.GetEmployeeById(
                        new GetEmployeeByIdRequest {
                        Id = id
                    }                                   //参数一:request参数(员工Id)
                        , metaData);                    //参数二:用户自定义的元数据
                    Response1 = response.ToString();    //将响应信息输出前台显示
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                //*********************************************************************
                return;
            }
            MessageBox.Show("request is unValid");
        }