简体   繁体   English

如果状态码不等于 200 OK 使用 Polly 重试 api 请求

[英]Use Polly to retry api request if the status code is not equal to 200 OK

I am trying to use Polly to handle status codes other than 200 OK so that it retries the API request.我正在尝试使用 Polly 处理 200 OK 以外的状态代码,以便它重试 API 请求。

However, I am struggling to understand how to Polly to swallow an exception and then execute a retry.但是,我很难理解如何让 Polly 吞下异常然后执行重试。 This is what I currently have, but it is not syntactically correct:这是我目前拥有的,但在语法上不正确:

var policy = Policy.HandleResult<HttpStatusCode>(r => r == HttpStatusCode.GatewayTimeout)
                                                        .Retry(5);
        HttpWebResponse response;

        policy.Execute(() => 
        {
            response = SendRequestToInitialiseApi(url);
        });

In the execution, I need the response to also be assigned to a variable to be used elsewhere in the method.在执行过程中,我需要将响应分配给一个变量,以便在方法的其他地方使用。

The return type of the response is a 'HttpWebResponse' and when I get a '504' for instance, it throws an exception.响应的返回类型是“HttpWebResponse”,例如,当我得到“504”时,它会引发异常。

Any help/feedback would be appreciated.任何帮助/反馈将不胜感激。

Cheers干杯

The type you provide to HandleResult must be the same type you want to return from the execution, which in your case is an HttpWebResponse since you want to make use of this value later.您提供给HandleResult的类型必须与您希望从执行中返回的类型相同,在您的情况下是HttpWebResponse因为您想稍后使用此值。

Edit: I've added in exception handling as suggested by mountain traveller编辑:我已经按照山地旅行者的建议添加了异常处理

var policy = Policy
    // Handle any `HttpRequestException` that occurs during execution.
    .Handle<HttpRequestException>()
    // Also consider any response that doesn't have a 200 status code to be a failure.
    .OrResult<HttpWebResponse>(r => r.StatusCode != HttpStatusCode.OK)
    .Retry(5);

// Execute the request within the retry policy and return the `HttpWebResponse`.
var response = policy.Execute(() => SendRequestToInitialiseApi(url));

// Do something with the response.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JQuery AJAX,错误状态代码:200,状态文本:parserorro | 好 - JQuery AJAX, Error Status code: 200, Status Text: parserorro | OK Restsharp polly 基于状态的重试策略 - Restsharp polly retry policy based on status Polly 使用不同的请求体重试请求 - Polly retry request with different request-body 在 Api 的 Ok ()(状态 200)中返回 json 的最佳方法是什么? - what is the best way to return a json in an Ok () (status 200) of an Api? 相同状态代码 200OK 的多个 ProducesResponseType - Multiple ProducesResponseType for same status code 200OK 使用 Polly 重试处理更长的时间间隔的 HTTP 请求错误是否安全 - Is it safe to use Polly retry to handle HTTP request errors for longer time intervals 带有OK(200)代码的CreatedAtRoute - CreatedAtRoute with OK(200) code 未经授权的消息,状态为200 OK - Unauthorized Message,Status 200 OK 为什么我的 Web api 在使用 WebClient 类时返回状态代码 401,而在使用 HttpWebResponse 时返回状态代码 200? - Why my web api return Status code 401 when i use WebClient class and Status code 200 when i use HttpWebResponse? 无论 KB 中是否存在匹配项,QnA Api 都会返回响应代码 200 OK - QnA Api returns response code 200 OK irrespective of no match in the KB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM