簡體   English   中英

自動 Rest API 客戶端 Visual Studio 不重試失敗

[英]Auto Rest API Client Visual Studio no retry on failure

I have a problem with the handling of the Rest Api Client that i created directly in visual studio based on my swagger api. (“添加”-> Rest Api 客戶端“) (C#)

當請求失敗時,客戶端會嘗試 5 次才最終拋出異常。

我如何確保客戶端將直接拋出異常而無需重試?

提前致謝

如果您為生成的 swagger 客戶端創建部分 class,則可以完成此操作。 確保在您的實現中匹配生成的客戶端的命名空間。 創建一個空的構造函數並設置重試策略。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM