Beispiel #1
0
        /// <summary>
        /// grpc方法调用
        /// </summary>
        public async Task <string> MethodInvoke(string endpoint, string methodName, string requestJson, Dictionary <string, string> customHeaders)
        {
            var channel = new Channel(endpoint, ChannelCredentials.Insecure);

            try
            {
                var client   = new BaseServiceClient(channel);
                var metadata = new Metadata();
                foreach (var item in customHeaders)
                {
                    if (!metadata.Any(p => p.Key == item.Key))
                    {
                        metadata.Add(new Metadata.Entry(item.Key, item.Value));
                    }
                }
                return((await client.MethodInvokeAsync(new MethodInvokeRQ
                {
                    FullName = methodName,
                    RequestJson = requestJson
                }, metadata
                                                       // , deadline: DateTime.UtcNow.AddSeconds(_grpcTimeout)
                                                       )).ResponseJson);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(ex.ToString());
            }
            finally
            {
                await channel.ShutdownAsync();
            }
        }
        /// <summary>
        /// grpc方法调用
        /// </summary>
        public async Task <string> MethodInvoke(string endpoint, string methodName, string requestJson)
        {
            var channel = new Channel(endpoint, ChannelCredentials.Insecure);

            try
            {
                var client = new BaseServiceClient(channel);
                return((await client.MethodInvokeAsync(new MethodInvokeRQ
                {
                    FullName = methodName,
                    RequestJson = requestJson
                } /*, deadline: DateTime.UtcNow.AddSeconds(_config.GetValue<int>("GrpcTimeout"))*/)).ResponseJson);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(ex.ToString());
            }
            finally
            {
                await channel.ShutdownAsync();
            }
        }