- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在调用 Pittney Bowes Geocoder 服务时使用 Polly 捕获异常。我正在使用抛出 MessageProcessingException 的 g1client 库。如果抛出此异常,我已将调用包装在 Polly 网络策略中以重试调用最多 3 次,但 Visual Studio 坚持认为该异常是“用户未处理”我需要修改什么才能处理此异常?我使用的是 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;
}
错误信息是:
g1client.MessageProcessingException occurred
HResult=0x80131500
Message=Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Client timeout
Source=g1client
StackTrace:
at g1client.UTF8Serializer.DeserializeHashtable(NetworkStream networkStream)
at g1client.UTF8Serializer.GetMessage(NetworkStream networkStream)
at g1client.SocketGatewayConnection.ProcessBytes(Byte[] bytesIn)
at g1client.SocketGatewayConnection.Process(Message message)
at g1client.RemoteService.Process(Message message)
at Midas.Core.PBGeocoder.Geocoder.<>c__DisplayClass27_0.<Bulk2PassGeocode>b__2() in E:\Source Code\GitHub\Midas.Core\Midas.Core.PBGeocoder\Geocoder.cs:line 321
at Polly.Policy.<>c__DisplayClass75_0`1.<Execute>b__0(CancellationToken ct)
at Polly.Policy.<>c__DisplayClass27_0`1.<Execute>b__0(CancellationToken ct)
at Polly.RetrySyntax.<>c__DisplayClass11_0.<WaitAndRetry>b__1(CancellationToken ct)
at Polly.Retry.RetryEngine.Implementation[TResult](Func`2 action, CancellationToken cancellationToken, IEnumerable`1 shouldRetryExceptionPredicates, IEnumerable`1 shouldRetryResultPredicates, Func`1 policyStateFactory)
我还想查明为什么会出现此错误,因此任何有助于调试的信息也都很棒。我只有在第一次以大请求调用该服务时才得到它。在这种情况下,我发送了 1000 个地址进行处理。下一次运行请求将在一秒钟内处理,但第一次它不起作用。
最佳答案
TL;DR 您只是看到 VS 调试器因异常而中断。
您可能将 Visual Studio 调试器的(本质上令人困惑的)术语用户未处理的异常理解为执行代码库作为一个整体没有处理异常;不是这种情况。
User-unhandled exception 在这种情况下意味着首先处理异常 other than by your code - 此处,根据 Polly 政策。
首先,Visual Studio 将您正在调试的代码分成 'my code' aka 'user code', and 'non-user code' .在您的场景中,Polly 和 Geocoder 库将被归类为“非用户代码”。
其次,默认情况下,Visual Studio 调试器会中断 [+] 由“非用户代码”处理的异常(但使用这个可能令人困惑和令人担忧的术语“用户-未处理”!) .
Visual Studio 默认中断 [+],以便您可以看到触发异常的行(即使后续代码将处理它...)。因此,听起来(调试器设置上的 dpdg)好像您正在看到 Visual Studio 中断,即使异常将按照 Polly 策略的配置进行处理。只需在调试器中按 F5/Continue,随后的代码就会恢复。
您还可以按照 instructions in this article under the heading Tell the debugger to continue on user-unhandled exceptions 更改此行为[+] .
您可以通过检查 System.Diagnostics.Debug.WriteLine("Exception being retried"+ exception);
行的行为来进一步确定 Polly 策略正在运行。如果正在调用重试,您应该会看到该行的输出 - 或者如果您在其上设置断点,则会看到它命中。
最后,Polly 重试策略 rethrows any final exception if all configured retries have been exhausted .这是有意的 - 表明可能发生 - 而不是失败。
您正确地为此设置了 try { ... } catch (MessageProcessingException ex) { ... }
,但调试器可能会再次误导性地将最终异常标记为“用户未处理”——甚至尽管您对它有一个 try-catch - 因为它最初被 Polly 捕获。
关于c# - 使用 Polly 时 VS 调试器中报告的异常用户未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057939/
我正在尝试为我的 Rest 客户创建一个综合弹性策略,为此我编写了以下内容: private static Policy circuitBreakerPolicy = Policy .Han
我以前从未使用过 Polly,不确定这是否适合 Polly。 我正在调用 POST 正文中包含 1000 个 DTO 列表的端点。现在,端点将对每个 DTO 执行一些验证,如果这些 DTO 中的任何一
我将 Polly 与 .net Core 结合使用。我的 ConfigureServices 是: private static void ConfigureServices() { var
我正在使用 .NET 5 并希望使用 Polly 在重试时更改请求的查询字符串。背景 - 我有一个固定的每分钟请求配额,这是我的 IP 地址所允许的。如果超过限制,我会得到一个特定的 4xx 状态代码
Polly 中是否有办法重试除指定异常之外的所有异常。例如: var p = Policy .Handle(e => !(e.NativeErrorCode == 1)) .Or()
我们正在考虑使用 Polly 作为我们的故障转移库。 我们在 kubernetes 环境中运行我们的应用程序。例如,我们无法保证针对特定服务运行重试尝试的 pod 将存活足够长的时间以使请求成功。如果
我正在尝试使超时策略正常工作。我在集成 api 时有以下需求。 创建一个 http 请求来调用 endpoint1 并传递 transactionID 并捕获结果 如果 http 请求在 20 秒内没
在启动过程中,我基本上添加了一个 HttpClient,如下所示: services.AddHttpClient().AddPolicyHandler(GetRetryPolicy()); publi
在策略定义中,我希望能够在运行时禁用或启用该策略,而不是在调用站点中进行,因为我可能有多个调用站点。 这是我目前的方法? private RetryPolicy GetRetryPolicy() {
我对波利很陌生。我做了一些研究,但仍然无法确定是否/如何仅在请求失败时以优雅的方式使用 Polly 来使用 chached 值。例子: 服务 A 想通过 http 从服务 B 获取数据。我总是想获取最
我正在使用 AWS Polly 服务进行文本转语音。但是如果文本包含一些特殊字符,它会返回错误的开始和结束编号。 例如,如果文本是:“Böylelikle”,它返回:{"time":6,"type":
我试图重试失败的操作 3 次。 我正在使用 Polly 进行重试操作。 我想在重试操作失败的情况下获得异常并重试 2 次,依此类推。 return await Policy .H
我已经阅读了 Polly 库的文档和示例,它真的很棒而且易于使用!! 就我而言,我想将所有异常分为 3 种类型:临时、永久和日志。现在,我想要一段代码,负责通过使用 Polly 库进行等待和重试来处理
我在 PolicyRegistry 中有以下政策可以在全局范围内重复使用: var fallbackPolicy = Policy .Handle().OrInner()
我正在尝试检查 Polly 的简单 RetryForever class Program { public static void Main() {
我已经阅读了 Polly 库的文档和示例,它真的很棒而且易于使用!! 就我而言,我想将所有异常分为 3 种类型:临时、永久和日志。现在,我想要一段代码,负责通过使用 Polly 库进行等待和重试来处理
我在 PolicyRegistry 中有以下政策可以在全局范围内重复使用: var fallbackPolicy = Policy .Handle().OrInner()
我正在使用一个非常不稳定的 API。有时我会收到 500 服务器错误 和 Timeout,有时我也会收到 500 服务器错误,因为我给它提供了它无法处理的输入 SqlDateTime overflow
我正在尝试检查 Polly 的简单 RetryForever class Program { public static void Main() {
我想执行某个操作,如果失败3次则返回null。 Polly 中类似的东西将是完美的: var results = await Policy> .Handle() .RetryAsync
我是一名优秀的程序员,十分优秀!