use*_*446 2 c# networking geocoding unhandled-exception 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 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)
Run Code Online (Sandbox Code Playgroud)
我也想跟踪我为什么会收到这个错误,所以任何帮助调试都会很好.我只是在第一次用大请求调用服务时才得到它.在这种情况下,我发送了1000个地址进行处理.下一次运行请求将在一秒钟内处理,但第一次它不起作用.
TL; DR您只是看到VS调试器在异常时中断.
您可能正在理解Visual Studio Debugger的(固有地令人困惑的)术语用户未处理的异常,这意味着执行的代码库作为一个整体不处理异常; 不是这种情况.
在这种情况下,用户未处理的异常意味着除了您的代码之外,首先处理异常- 这里是Polly策略.
首先,Visual Studio将您正在调试的代码分为"我的代码",即"用户代码"和"非用户代码".在您的场景中,Polly和Geocoder库将被归类为"非用户代码".
其次,默认情况下,Visual Studio调试器符[+]由"非用户代码"处理的异常(但使用这种潜在的混乱和令人震惊的术语"用户未办理"!).
默认情况下,Visual Studio会中断[+],以便您可以看到触发异常的行(即使后续代码将处理它...).因此,它听起来(调试器设置上的dpdg)就像您看到Visual Studio中断一样,即使异常将按照Polly策略的配置进行处理.只需在调试器中按F5/Continue,随后的代码将恢复.
您还可以按照本文中的标题告诉调试器继续处理用户未处理的异常,更改此行为[+] .
您可以通过检查线路的行为来进一步满足自己的Polly政策System.Diagnostics.Debug.WriteLine("Exception being retried" + exception);.您应该看到该行的输出 - 或者如果您在其上设置断点,则会看到它 - 如果正在调用重试.
最后,如果所有已配置的重试都已用尽,则Polly重试策略将重新抛出任何最终异常.这是故意的 - 表明可能性 - 而不是失败.
你正确地得到了try { ... } catch (MessageProcessingException ex) { ... }这个,但是调试器可能再次误导性地标记最终异常'user-unhandled' - 即使你有一个try-catch - 因为它最初被Polly捕获.