【问题标题】:Replay an HttpCall From Flurl从 Flurl 重放 HttpCall
【发布时间】:2018-11-17 19:02:58
【问题描述】:

我正在编写一个错误处理程序来处理令牌刷新。当我收到 expired_token 错误时,我正在刷新令牌,我想重播请求,但我不确定如何

public async Task HandleErrorAsync(HttpCall call)
{
      var exception = call.Exception;
      if (exception is FlurlHttpException)
      {
         FlurlHttpException ex = (exception as FlurlHttpException);
         var errorResponse = await ex.GetResponseJsonAsync<ErrorResponse>();

         if(errorResponse.Errors.Any(x => x.Id == EXPIRED_TOKEN))
         {
             await this.RefreshOAuthToken();
             //How can I Replay the request
             //call.Response = call.Request.Replay(); 
             call.ExceptionHandled = true;
         }
     }
}

刷新令牌后,我可以访问刚刚抛出过期令牌错误的 HttpCall 对象。我想重播请求并替换响应,但我不知道该怎么做。

如何在 Flurl 中重放来自 HttpCall 的请求?

【问题讨论】:

    标签: c# .net error-handling flurl


    【解决方案1】:

    我发现一般发送请求的重载,我做了一个扩展方法

    public static async Task<HttpCall> Replay(this HttpCall call)
    {
        call.Response = await call.FlurlRequest.SendAsync(call.Request.Method, call.Request.Content);
        return call;
    }
    

    【讨论】:

      【解决方案2】:

      也许你想看看名为 polly 的 c# 库。我认为它解决了开发人员面临的大部分重试问题。 https://github.com/App-vNext/Polly

      您可以使用 polly 的重试策略或其他适合您需要的策略

              // Retry multiple times, calling an action on each retry 
      // with the current exception and retry count
      Policy
          .Handle<SomeExceptionType>()
          .Retry(3, (exception, retryCount) =>
          {
              // do something 
          });
      

      【讨论】:

      • 非常感谢。我调整了答案并学到了一些东西:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多