C#Polly用于处理错误http响应

C#Polly用于处理错误http响应,c#,http,polly,C#,Http,Polly,我试图在构造函数中实现用于处理HTTP错误状态的Polly,但我得到一个错误,即:非泛型方法不能与类型参数一起使用。我使用了他们网站上的代码。这是: public BaseApiManager() { Retry = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)

我试图在构造函数中实现用于处理HTTP错误状态的Polly,但我得到一个错误,即:非泛型方法不能与类型参数一起使用。我使用了他们网站上的代码。这是:

 public BaseApiManager()
        {
           Retry = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
                        .OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.BadGateway)
                        .WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(5));
        }
public baseapimager()
{
Retry=Policy.HandleResult(r=>r.StatusCode==HttpStatusCode.InternalServerError)
.OrResult(r=>r.StatusCode==HttpStatusCode.BadGateway)
.WaitAndRetry(3,retrytry=>TimeSpan.FromSeconds(5));
}
错误在第二行的“OrResult”处。我还尝试将它与ApiResponse一起使用,这是我的泛型类,但效果并不理想。
我做错了什么?谢谢。

更正您的重试定义,指定类型:

private RetryPolicy<HttpResponseMessage> Retry { get; }
private RetryPolicy重试{get;}

它的工作原理如下:

Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
                    .OrResult(r => r.StatusCode == HttpStatusCode.BadGateway)
                    .WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(5));
Policy.HandleResult(r=>r.StatusCode==HttpStatusCode.InternalServerError)
.OrResult(r=>r.StatusCode==HttpStatusCode.BadGateway)
.WaitAndRetry(3,retrytry=>TimeSpan.FromSeconds(5));

.OrResult(…)
不需要泛型类型参数,因为
.Handle(…)
子句已将策略绑定为处理类型为
HttpResponseMessage
的结果

如果我使用bool,它就不允许我使用r.statuscode查看文档:您的代码似乎还可以。但也许您有名为HttpResponseMessage的方法?不,我使用默认方法。但是它的行为就像它想要一个bool,我还有上面的声明:private RetryPolicy Retry{get;}Polly网站上的文档中有一个错误。现在已经纠正了。