我知道我可以指定 HTTP 错误代码列表(例如 408、502、503 等),我想使用Polly重试,但是如果未指定,默认情况下将重试的这些代码的列表是什么?
我正在使用一个非常脆弱的 API。有时我500 Server Error
用Timeout
,其他时间我还可以得到500 Server Error
,因为我给它输入,它不能处理SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
。
这两种情况都给了我,HttpRequestException
但我可以查看来自服务器的回复消息并确定异常的原因。如果是超时错误,我应该再试一次。如果这是一个错误的输入,我应该重新抛出异常,因为再多的重试也无法解决错误数据的问题。
我想对 Polly 做的是在尝试重试之前检查响应消息。但是到目前为止我看到的所有样本都只包含异常类型。
到目前为止,我想出了这个:
HttpResponseMessage response = null;
String stringContent = null;
Policy.Handle<FlakyApiException>()
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
async (exception, timeSpan, context) =>
{
response = await client.PostAsync(requestUri, new StringContent(serialisedParameters, Encoding.UTF8, "application/json"));
stringContent = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.InternalServerError && stringContent.Contains("Timeout"))
{
throw new FlakyApiException(stringContent);
}
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来进行这种检查?
我已经阅读了使用HttpClientFactory 的热门博客文章https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore
引用它
ASP.NET Core 2.1中将出现一个新的HttpClientFactory功能,它有助于解决开发人员在使用HttpClient实例从其应用程序发出外部Web请求时可能遇到的一些常见问题.
所有示例都显示在asp.net应用程序的启动类中连接它,例如
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpClient();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是你可以在ASP.NET核心之外使用吗?如果是这样的例子
我会想到很多非Web应用程序(.net核心应用程序)需要进行Web调用,那么为什么这不是.net core api的一部分而不是放入asp.net core api
我正在实施 Polly 以在我的 C# Web 应用程序中重试请求。我的示例代码包含在这篇文章中。代码按预期工作,但传递给 CreateFile() 的最后一个参数(当前硬编码为 0)需要是 retryAttempt 的值。如何在执行操作中获取 retryAttempt 的值?
return Policy
.Handle<HttpException>(x => x.StatusCode == (HttpStatusCode)429)
.Or<StorageException>()
.WaitAndRetry(maxRetryCount, retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(retryIntervalMs, retryAttempt)))
.Execute(() => CreateFile(fileContent, containerName, fileName, connectionString, 0));
Run Code Online (Sandbox Code Playgroud) 如何识别Polly中最终重试的完成(不是onRetry事件,而是实际执行和完成)?
我可以将重试计数与 onRetry 事件内的最大重试计数进行比较,但该事件只是重试的启动,而重试在等待时间内尚未发生。我想要做的是确定最终重试的结束,无论是成功还是失败。
我正在使用 Amazon Polly 进行 TTS,但我无法了解如何将转换后的语音保存到我的计算机中的 .mp3 文件中
我已经尝试过 gTTS,但我的任务需要 Amazon Polly。
import boto3
client = boto3.client('polly')
response = client.synthesize_speech
(Text = "Hello my name is Shubham", OuptutFormat = "mp3", VoiceId = 'Aditi')
Run Code Online (Sandbox Code Playgroud)
现在,我应该怎么做才能播放此转换后的语音或将其作为 .mp3 文件保存到我的 PC 中?
python text-to-speech amazon-web-services boto3 amazon-polly
在调用Pittney Bowes Geocoder服务时,我正在使用Polly来捕捉异常.我正在使用一个抛出MessageProcessingException的g1client库.如果抛出此异常,我已经将调用包装在Polly网络策略中以重试调用最多3次,但Visual Studio坚持异常为"User-Unhandled"我需要修改哪些内容才能处理此异常?我正在使用Visual Studio 2017社区版和C#4.6.1.
try
{
var networkPolicy = Policy
.Handle<MessageProcessingException>()
.Or<Exception>()
.WaitAndRetry(
3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(exception, timeSpan, context) =>
{
System.Diagnostics.Debug.WriteLine("Exception being retried" + exception);
});
geocoderResponse = networkPolicy.Execute(() => geocoderService.Process(geocoderRequest));
}
catch (MessageProcessingException ex)
{
log.Error("MessageProcessingException calling the Geocoder service", ex);
throw ex;
}
catch (Exception ex)
{
log.Error("Exception calling the Geocoder service", ex);
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
错误消息是:
g1client.MessageProcessingException occurred
HResult=0x80131500
Message=Unable to read data from the transport connection: A connection attempt failed because …
Run Code Online (Sandbox Code Playgroud) 我想执行某个操作,如果失败三次返回null。在 Polly 中这样的东西将是完美的:
var results = await Policy<IList<Value>>
.Handle<TaskCanceledException>()
.RetryAsync<IList<Value>>(3)
.FallbackAsync(null as IList<Value>)
.ExecuteAsync(() => myRestfulCall());
Run Code Online (Sandbox Code Playgroud)
这是不可能的,因为RetryAsync
返回 anAsyncRetryPolicy
并且没有在此类型上定义 Fallback 扩展方法。是否有不需要 try/catch 块的 Polly 语法来执行此操作?
polly ×6
c# ×5
.net ×1
.net-core ×1
amazon-polly ×1
asp.net ×1
asp.net-core ×1
boto3 ×1
geocoding ×1
networking ×1
python ×1
retry-logic ×1
retrypolicy ×1