protected virtual async Task SendAsync(FeignClientHttpRequest request, Func <Task> fallback)
        {
            try
            {
                await SendAsync(request)
#if CONFIGUREAWAIT_FALSE
                .ConfigureAwait(false)
#endif
                ;
            }
            catch (TerminatedRequestException)
            {
                return;
            }
            catch (Exception ex)
            {
                if (fallback == null)
                {
                    throw;
                }
                if (InvokeFallbackRequestPipeline(request, fallback, ex as ServiceResolveFailException))
                {
                    throw;
                }
                await fallback.Invoke()
#if CONFIGUREAWAIT_FALSE
                .ConfigureAwait(false)
#endif
                ;
            }
        }
Esempio n. 2
0
        private async Task <HttpResponseMessage> GetResponseMessageAsync(FeignClientHttpRequest request)
        {
            try
            {
                return(await SendAsyncInternal(request)
#if CONFIGUREAWAIT_FALSE
                       .ConfigureAwait(false)
#endif
                       );
            }
            catch (TerminatedRequestException)
            {
                if (IsResponseTerminatedRequest)
                {
                    return(null);
                }
                throw;
            }
            catch (ServiceResolveFailException)
            {
                throw;
            }
            catch (Exception ex)
            {
                #region ErrorRequest
                ErrorRequestEventArgs <TService> errorArgs = new ErrorRequestEventArgs <TService>(this, ex);
                OnErrorRequest(errorArgs);
                if (errorArgs.ExceptionHandled)
                {
                    return(null);
                }
                #endregion
                throw;
            }
        }
Esempio n. 3
0
 private HttpResponseMessage GetResponseMessage(FeignClientHttpRequest request)
 {
     try
     {
         return(SendAsyncInternal(request).GetResult());
     }
     catch (TerminatedRequestException)
     {
         if (IsResponseTerminatedRequest)
         {
             return(null);
         }
         throw;
     }
     catch (ServiceResolveFailException)
     {
         throw;
     }
     catch (Exception ex)
     {
         #region ErrorRequest
         ErrorRequestEventArgs <TService> errorArgs = new ErrorRequestEventArgs <TService>(this, ex);
         OnErrorRequest(errorArgs);
         if (errorArgs.ExceptionHandled)
         {
             return(null);
         }
         #endregion
         throw;
     }
 }
 internal FallbackRequestEventArgs(IFeignClient <TService> feignClient, FeignClientHttpRequest request, TService fallback, IFallbackProxy fallbackProxy, MethodInfo method) : base(feignClient)
 {
     Request       = request;
     Fallback      = fallback;
     FallbackProxy = fallbackProxy;
     _method       = method;
 }
 internal BuildingRequestEventArgs(IFeignClient <TService> feignClient, string method, Uri requestUri, IDictionary <string, string> headers, FeignClientHttpRequest request) : base(feignClient)
 {
     Method     = method;
     RequestUri = requestUri;
     Headers    = headers;
     Request    = request;
 }
Esempio n. 6
0
        private HttpRequestMessage CreateRequestMessage(FeignClientHttpRequest request, HttpMethod method, Uri uri)
        {
            FeignHttpRequestMessage requestMessage = new FeignHttpRequestMessage(request, method, uri);

            if (!string.IsNullOrWhiteSpace(request.Accept))
            {
                requestMessage.Headers.Accept.ParseAdd(request.Accept);
            }
            if (request.Headers != null && request.Headers.Length > 0)
            {
                foreach (var header in request.Headers)
                {
                    string[] values = header.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (values.Length == 2)
                    {
                        requestMessage.Headers.TryAddWithoutValidation(values[0], values[1]);
                    }
                }
            }
            if (request.RequestHeaderHandlers != null && request.RequestHeaderHandlers.Count > 0)
            {
                foreach (var handler in request.RequestHeaderHandlers)
                {
                    handler.SetHeader(requestMessage);
                }
            }
            return(requestMessage);
        }
        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));
                }
            }
        }
Esempio n. 8
0
        protected virtual TResult Send <TResult>(FeignClientHttpRequest request)
        {
            HttpResponseMessage response = GetResponseMessage(request);

            using (response)
            {
                return(GetResult <TResult>(request, response));
            }
        }
Esempio n. 9
0
        protected virtual void Send(FeignClientHttpRequest request)
        {
            HttpResponseMessage response = GetResponseMessage(request);

            using (response)
            {
                //GetResult<string>(request, response);
                EnsureSuccess(request, response);
            }
        }
Esempio n. 10
0
        public Task <string> Get()
        {
            FeignClientHttpRequest feignClientHttpRequest = new FeignClientHttpRequest(null, null, null, null, null)
            {
                CompletionOption = HttpCompletionOption.ResponseHeadersRead
            };
            var r = feignClientHttpRequest.ContentType;

            //HttpCompletionOption = HttpCompletionOption.ResponseHeadersRead;
            return(Task.FromResult("fallback_get"));
        }
Esempio n. 11
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));
                }
            }
        }
Esempio n. 12
0
 /// <summary>
 /// 确保响应状态正确
 /// </summary>
 /// <param name="request"></param>
 /// <param name="responseMessage"></param>
 private void EnsureSuccess(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
 {
     if (!responseMessage.IsSuccessStatusCode)
     {
         string content = responseMessage.Content.ReadAsStringAsync().GetResult();
         _logger?.LogError($"request on \"{responseMessage.RequestMessage.RequestUri.ToString()}\" status code : " + responseMessage.StatusCode.GetHashCode() + " content : " + content);
         throw new FeignHttpRequestException(this,
                                             responseMessage.RequestMessage as FeignHttpRequestMessage,
                                             new HttpRequestException($"Response status code does not indicate success: {responseMessage.StatusCode.GetHashCode()} ({responseMessage.ReasonPhrase}).\r\nContent : {content}"));
     }
 }
Esempio n. 13
0
        public Task <JObject> GetValueAsync([PathVariable] int id, [RequestParam] string test)
        {
            FeignClientMethodInfo feignClientMethodInfo = new FeignClientMethodInfo();

            feignClientMethodInfo.MethodId = "GetById";
            if (IncludeMethodMetadata)
            {
                feignClientMethodInfo.MethodMetadata = typeof(FeignClientMethodInfo).GetMethod("");
            }
            FeignClientHttpRequest request = new FeignClientHttpRequest("", "", "", "", "");

            throw new NotImplementedException();
        }
Esempio n. 14
0
        /// <summary>
        /// 确保响应状态正确
        /// </summary>
        /// <param name="request"></param>
        /// <param name="responseMessage"></param>
        private async Task EnsureSuccessAsync(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
        {
            if (!responseMessage.IsSuccessStatusCode)
            {
                string content = await responseMessage.Content.ReadAsStringAsync()
#if CONFIGUREAWAIT_FALSE
                                 .ConfigureAwait(false)
#endif
                ;

                _logger?.LogError($"request on \"{responseMessage.RequestMessage.RequestUri.ToString()}\" status code : " + responseMessage.StatusCode.GetHashCode() + " content : " + content);
                throw new FeignHttpRequestException(this,
                                                    responseMessage.RequestMessage as FeignHttpRequestMessage,
                                                    new HttpRequestException($"Response status code does not indicate success: {responseMessage.StatusCode.GetHashCode()} ({responseMessage.ReasonPhrase}).\r\nContent : {content}"));
            }
        }
Esempio n. 15
0
        public void TestMethod1()
        {
            FeignClientHttpRequest request = new FeignClientHttpRequest(null, null, null, null, null);

            request.RequestHeaderHandlers = new List <Request.Headers.IRequestHeaderHandler>();
            Request.Headers.IRequestHeaderHandler requestHeaderHandler = null;
            request.RequestHeaderHandlers.Add(requestHeaderHandler);
            DynamicAssembly dynamicAssembly = new DynamicAssembly();

            dynamicAssembly.DEBUG_MODE = true;
            FeignClientHttpProxyTypeBuilder feignClientTypeBuilder = new FeignClientHttpProxyTypeBuilder(dynamicAssembly);
            FeignClientTypeInfo             feignClientTypeInfo    = feignClientTypeBuilder.Build(typeof(ITestService));
            FeignClientTypeInfo             feignClientTypeInfo1   = feignClientTypeBuilder.Build(typeof(ITestControllerService));

            feignClientTypeBuilder.Save();
        }
Esempio n. 16
0
        protected virtual async Task <TResult> SendAsync <TResult>(FeignClientHttpRequest request)
        {
            HttpResponseMessage response = await GetResponseMessageAsync(request)
#if CONFIGUREAWAIT_FALSE
                                           .ConfigureAwait(false)
#endif
            ;

            using (response)
            {
                return(await GetResultAsync <TResult>(request, response)
#if CONFIGUREAWAIT_FALSE
                       .ConfigureAwait(false)
#endif
                       );
            }
        }
        /// <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));
        }
        /// <summary>
        /// 触发服务降级事件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="delegate"></param>
        /// <param name="serviceResolveFailException"></param>
        /// <returns></returns>
        bool InvokeFallbackRequestPipeline(FeignClientHttpRequest request, Delegate @delegate, ServiceResolveFailException serviceResolveFailException)
        {
            IFallbackProxy fallbackProxy = @delegate.Target as IFallbackProxy;
            FallbackRequestEventArgs <TService> eventArgs;

            if (fallbackProxy == null)
            {
                //可能因为method parameters length=0 , 故没有生成匿名调用类
                eventArgs = new FallbackRequestEventArgs <TService>(this, request, Fallback, null, @delegate.Method);
            }
            else
            {
                eventArgs = new FallbackRequestEventArgs <TService>(this, request, Fallback, fallbackProxy, null);
            }
            OnFallbackRequest(eventArgs);
            return(eventArgs.IsTerminated);
        }
Esempio n. 19
0
        protected virtual async Task SendAsync(FeignClientHttpRequest request)
        {
            HttpResponseMessage response = await GetResponseMessageAsync(request)
#if  CONFIGUREAWAIT_FALSE
                                           .ConfigureAwait(false)
#endif
            ;

            using (response)
            {
                //            await GetResultAsync<string>(request, response)
                //#if CONFIGUREAWAIT_FALSE
                //           .ConfigureAwait(false)
                //#endif
                //                ;
                await EnsureSuccessAsync(request, response)
#if CONFIGUREAWAIT_FALSE
                .ConfigureAwait(false)
#endif
                ;
            }
        }
 protected virtual TResult Send <TResult>(FeignClientHttpRequest request, Func <TResult> fallback)
 {
     try
     {
         return(Send <TResult>(request));
     }
     catch (TerminatedRequestException)
     {
         return(default(TResult));
     }
     catch (Exception ex)
     {
         if (fallback == null)
         {
             throw;
         }
         if (InvokeFallbackRequestPipeline(request, fallback, ex as ServiceResolveFailException))
         {
             throw;
         }
         return(fallback.Invoke());
     }
 }
Esempio n. 21
0
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private async Task <HttpResponseMessage> SendAsyncInternal(FeignClientHttpRequest request)
        {
            HttpMethod         httpMethod         = GetHttpMethod(request.HttpMethod);
            HttpRequestMessage httpRequestMessage = CreateRequestMessage(request, httpMethod, CreateUri(BuildUri(request.Uri)));

            using (httpRequestMessage)
            {
                // if support content
                if (IsSupportContent(httpMethod))
                {
                    HttpContent httpContent = request.GetHttpContent();
                    if (httpContent != null)
                    {
                        httpRequestMessage.Content = httpContent;
                    }
                }
                return(await HttpClient.SendAsync(httpRequestMessage, request.CompletionOption)
#if CONFIGUREAWAIT_FALSE
                       .ConfigureAwait(false)
#endif
                       );
            }
        }
 protected virtual void Send(FeignClientHttpRequest request, Action fallback)
 {
     try
     {
         Send(request);
     }
     catch (TerminatedRequestException)
     {
         return;
     }
     catch (Exception ex)
     {
         if (fallback == null)
         {
             throw;
         }
         if (InvokeFallbackRequestPipeline(request, fallback, ex as ServiceResolveFailException))
         {
             throw;
         }
         fallback.Invoke();
     }
 }
Esempio n. 23
0
 internal ReceivingResponseEventArgs(IFeignClient <TService> feignClient, FeignClientHttpRequest request, HttpResponseMessage responseMessage, Type resultType) : base(feignClient)
 {
     Request         = request;
     ResponseMessage = responseMessage;
     ResultType      = resultType;
 }
        private ReceivingResponseEventArgs <TService, TResult> InvokeReceivingResponseEvent <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage)
        {
            ReceivingResponseEventArgs <TService, TResult> receivingResponseEventArgs = new ReceivingResponseEventArgs <TService, TResult>(this, request, responseMessage);

            OnReceivingResponse(receivingResponseEventArgs);
            return(receivingResponseEventArgs);
        }