TResult GetResult <TResult>(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)
     {
         return((TResult)receivingResponseEventArgs.Result);
     }
     #endregion
     EnsureSuccess(responseMessage);
     return(Newtonsoft.Json.JsonConvert.DeserializeObject <TResult>(responseMessage.Content.ReadAsStringAsync().GetResult()));
 }
 HttpResponseMessage GetResponseMessage(Func <HttpResponseMessage> action)
 {
     try
     {
         return(action());
     }
     catch (Exception ex)
     {
         #region ErrorRequest
         ErrorRequestEventArgs errorArgs = new ErrorRequestEventArgs(this, ex);
         _globalFeignClientPipeline.GetServicePipeline(this.ServiceId)?.OnErrorRequest(this, errorArgs);
         if (!errorArgs.ExceptionHandled)
         {
             _globalFeignClientPipeline?.OnErrorRequest(this, errorArgs);
         }
         if (errorArgs.ExceptionHandled)
         {
             return(null);
         }
         #endregion
         throw;
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var current = request.RequestUri;
            ServiceFeignClientPipelineBuilder serviceFeignClientPipeline = _globalFeignClientPipeline?.GetServicePipeline(_feignClient.ServiceId);

            try
            {
                #region BuildingRequest
                BuildingRequestEventArgs buildingArgs = new BuildingRequestEventArgs(_feignClient, request.Method.ToString(), request.RequestUri, new Dictionary <string, string>());

                serviceFeignClientPipeline?.OnBuildingRequest(_feignClient, buildingArgs);
                _globalFeignClientPipeline?.OnBuildingRequest(_feignClient, buildingArgs);
                //request.Method = new HttpMethod(buildingArgs.Method);
                request.RequestUri = buildingArgs.RequestUri;
                if (buildingArgs.Headers != null && buildingArgs.Headers.Count > 0)
                {
                    foreach (var item in buildingArgs.Headers)
                    {
                        request.Headers.TryAddWithoutValidation(item.Key, item.Value);
                    }
                }
                #endregion
                request.RequestUri = LookupService(request.RequestUri);
                #region SendingRequest
                SendingRequestEventArgs sendingArgs = new SendingRequestEventArgs(_feignClient, request);
                serviceFeignClientPipeline?.OnSendingRequest(_feignClient, sendingArgs);
                _globalFeignClientPipeline?.OnSendingRequest(_feignClient, sendingArgs);
                request = sendingArgs.RequestMessage;
                #endregion

                #region CannelRequest
                CancelRequestEventArgs cancelArgs = new CancelRequestEventArgs(_feignClient, cancellationToken);
                serviceFeignClientPipeline?.OnCancelRequest(_feignClient, cancelArgs);
                _globalFeignClientPipeline?.OnCancelRequest(_feignClient, cancelArgs);
                #endregion

                return(await base.SendAsync(request, cancellationToken));
            }
            catch (Exception e)
            {
                _logger?.LogDebug(e, "Exception during SendAsync()");
                throw;
            }
            finally
            {
                request.RequestUri = current;
            }
        }