gpt4 book ai didi

c# - 使用 Polly 时 VS 调试器中报告的异常用户未处理

转载 作者:行者123 更新时间:2023-11-30 19:54:04 24 4
gpt4 key购买 nike

我在调用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com