/// <summary>
        /// 获取响应结果
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="request"></param>
        /// <param name="responseMessage"></param>
        /// <returns></returns>
        private Task <TResult> GetResultAsync <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
        {
            if (responseMessage == null)
            {
                return(Task.FromResult(default(TResult)));
            }
            #region ReceivingResponse
            ReceivingResponseEventArgs <TService, TResult> receivingResponseEventArgs = InvokeReceivingResponseEvent <TResult>(request, responseMessage);
            //if (receivingResponseEventArgs.Result != null)
            if (receivingResponseEventArgs._isSetResult)
            {
                return(Task.FromResult(receivingResponseEventArgs.GetResult()));
            }
            #endregion

            return(GetResultInternalAsync <TResult>(request, responseMessage));
        }
Example #2
0
        private async Task <TResult> GetResultAsync <TResult>(FeignClientRequest request, HttpResponseMessage responseMessage)
        {
            if (responseMessage == null)
            {
                return(default(TResult));
            }
            #region ReceivingResponse
            ReceivingResponseEventArgs <TResult> receivingResponseEventArgs = new ReceivingResponseEventArgs <TResult>(this, responseMessage);
            _globalFeignClientPipeline?.GetServicePipeline(this.ServiceId)?.OnReceivingResponse(this, receivingResponseEventArgs);
            _globalFeignClientPipeline?.OnReceivingResponse(this, receivingResponseEventArgs);
            //if (receivingResponseEventArgs.Result != null)
            if (receivingResponseEventArgs._isSetResult)
            {
                return(receivingResponseEventArgs.GetResult <TResult>());
            }
            #endregion
            await EnsureSuccessAsync(request, responseMessage);

            if (typeof(TResult) == typeof(Task))
            {
#if NET45
                return((TResult)(object)Task.FromResult <object>(null));
#endif
#if NETSTANDARD
                return((TResult)(object)Task.CompletedTask);
#endif
            }
            if (typeof(TResult) == typeof(string))
            {
                return((TResult)(object)await responseMessage.Content.ReadAsStringAsync());
            }
            IMediaTypeFormatter mediaTypeFormatter = _feignOptions.MediaTypeFormatters.FindFormatter(responseMessage.Content.Headers.ContentType?.MediaType);
            if (mediaTypeFormatter == null)
            {
                throw new FeignHttpRequestException(this,
                                                    responseMessage.RequestMessage as FeignHttpRequestMessage,
                                                    new HttpRequestException($"Content type '{responseMessage.Content.Headers.ContentType.ToString()}' not supported"));
            }
            return(mediaTypeFormatter.GetResult <TResult>(await responseMessage.Content.ReadAsByteArrayAsync(), FeignClientUtils.GetEncoding(responseMessage.Content.Headers.ContentType)));
        }