【问题标题】:Auto Rest API Client Visual Studio no retry on failureAuto Rest API 客户端 Visual Studio 不重试失败
【发布时间】:2020-04-15 17:10:16
【问题描述】:

我在处理基于我的 swagger api 直接在 Visual Studio 中创建的 Rest Api 客户端时遇到问题。 ("添加" -> Rest Api 客户端") (C#)

当请求失败时,客户端会尝试 5 次才最终抛出异常。

如何确保客户端直接抛出异常而不重试?

提前致谢

【问题讨论】:

  • 您可以使用 fiddler 来检查来自您客户的请求。 telerik.com/fiddler
  • 我知道提琴手,但这不是我要求的。我想知道如何避免创建的客户端在失败时重试
  • 太好了,fiddler 是一个 web 调试代理,现在你应该知道如何触发你的重试,在这种情况下,你将使用 fiddler 来拦截请求,如果你只有一个request 这意味着重试确实有效。运气

标签: c# .net rest swagger


【解决方案1】:

如果您创建生成的 Swagger 客户端的部分类,您可以完成此操作。确保在您的实现中匹配生成的客户端的命名空间。创建一个空的构造函数并设置重试策略。

namesapce <Generated Client Namespace>
{
    public partial class MyClient
    {
        public MyClient()
        {
            this.SetRetryPolicy(new RetryPolicy(new ErrorRetryStrategy(), 0));
        }
    }
}

0 代表重试次数。最重要的是,重试策略的定义总是返回一个 false 以便短路重试。

您还需要实现 ErrorRetryStrategy()

public class ErrorRetryStrategy : ITransientErrorDetectionStrategy
{
    public bool IsTransient(Exception ex)
    {
        return false;
    }
}

另一种实现方式是自定义返回。

public class RetryStrategy : ITransientErrorDetectionStrategy
{
    public bool IsTransient(Exception ex)
    {
            return ex is HttpRequestException;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2019-10-08
    相关资源
    最近更新 更多