private TResult GetResultInternal <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
        {
            EnsureSuccess(request, responseMessage);
            var specialResult = SpecialResults.GetSpecialResult <TResult>(responseMessage);

            if (specialResult.IsSpecialResult)
            {
                return(specialResult.Result);
            }
            if (responseMessage.Content.Headers.ContentType == null && responseMessage.Content.Headers.ContentLength == 0)
            {
                return(default(TResult));
            }
            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"));
            }
            using (var stream = responseMessage.Content.ReadAsStreamAsync().GetResult())
            {
                if (stream.CanSeek)
                {
                    return(GetResultInternal <TResult>(mediaTypeFormatter, stream, responseMessage.Content.Headers.ContentType, request.Method.ResultType));
                }
                using (Stream seekStream = new MemoryStream())
                {
                    stream.CopyTo(seekStream);
                    seekStream.Position = 0;
                    return(GetResultInternal <TResult>(mediaTypeFormatter, seekStream, responseMessage.Content.Headers.ContentType, request.Method.ResultType));
                }
            }
        }
예제 #2
0
        private async Task <TResult> GetResultInternalAsync <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
        {
            await EnsureSuccessAsync(request, responseMessage)
#if CONFIGUREAWAIT_FALSE
            .ConfigureAwait(false)
#endif
            ;

            var specialResult = await SpecialResults.GetSpecialResultAsync <TResult>(responseMessage)
#if CONFIGUREAWAIT_FALSE
                                .ConfigureAwait(false)
#endif
            ;

            if (specialResult.IsSpecialResult)
            {
                return(specialResult.Result);
            }

            if (responseMessage.Content.Headers.ContentType == null && responseMessage.Content.Headers.ContentLength == 0)
            {
                return(default(TResult));
            }
            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"));
            }

            using (var stream = await responseMessage.Content.ReadAsStreamAsync()
#if CONFIGUREAWAIT_FALSE
                                .ConfigureAwait(false)
#endif
                   )
            {
                if (stream.CanSeek)
                {
                    return(GetResultInternal <TResult>(mediaTypeFormatter, stream, responseMessage.Content.Headers.ContentType, request.Method.ResultType));
                }
                using (Stream seekStream = new MemoryStream())
                {
                    await stream.CopyToAsync(seekStream)
#if CONFIGUREAWAIT_FALSE
                    .ConfigureAwait(false)
#endif
                    ;

                    seekStream.Position = 0;
                    return(GetResultInternal <TResult>(mediaTypeFormatter, seekStream, responseMessage.Content.Headers.ContentType, request.Method.ResultType));
                }
            }
        }