使用 Polly 时 VS 调试器中报告的异常用户未处理
Exception User-Unhandled reported in VS Debugger when using 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;
}
错误信息是:
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 调试器的(本质上令人困惑的)术语 用户未处理的异常 意味着正在执行的代码库作为一个整体没有处理异常;不是这种情况。
用户未处理的异常 在这种情况下意味着首先处理异常 other than by your code - 此处,由 Polly 策略处理。
首先,Visual Studio 将您正在调试的代码分成 'my code' aka 'user code', and 'non-user code'。在您的场景中,Polly 和 Geocoder 库将被分类 'non-user code'。
其次,默认情况下,Visual Studio 调试器会中断 [+] 由 'non-user code' 处理的异常(但使用这个可能令人困惑和震惊的术语 'user-unhandled'!)。
Visual Studio 默认情况下会中断 [+] 以便您可以 查看 触发异常的行(即使后续代码将处理它...... .).因此,听起来(调试器设置上的 dpdg)好像您正在看到 Visual Studio 中断,即使异常将按照 Polly 策略的配置进行处理。只需在调试器中按F5/Continue,后面的代码就会恢复。
更改此行为[+]
您可以通过检查行 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) { ... }
,但调试器可能会再次误导性地标记最终异常 'user-unhandled' - 即使你有一个 try-catch - 因为它最初被捕获通过波莉。
我在调用 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;
}
错误信息是:
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 调试器的(本质上令人困惑的)术语 用户未处理的异常 意味着正在执行的代码库作为一个整体没有处理异常;不是这种情况。
用户未处理的异常 在这种情况下意味着首先处理异常 other than by your code - 此处,由 Polly 策略处理。
首先,Visual Studio 将您正在调试的代码分成 'my code' aka 'user code', and 'non-user code'。在您的场景中,Polly 和 Geocoder 库将被分类 'non-user code'。
其次,默认情况下,Visual Studio 调试器会中断 [+] 由 'non-user code' 处理的异常(但使用这个可能令人困惑和震惊的术语 'user-unhandled'!)。
Visual Studio 默认情况下会中断 [+] 以便您可以 查看 触发异常的行(即使后续代码将处理它...... .).因此,听起来(调试器设置上的 dpdg)好像您正在看到 Visual Studio 中断,即使异常将按照 Polly 策略的配置进行处理。只需在调试器中按F5/Continue,后面的代码就会恢复。
更改此行为[+]您可以通过检查行 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) { ... }
,但调试器可能会再次误导性地标记最终异常 'user-unhandled' - 即使你有一个 try-catch - 因为它最初被捕获通过波莉。