2017-05-18 35 views
0

我正在使用Polly在调用Pittney Bowes Geocoder服务时捕获异常。我正在使用引发MessageProcessingException的g1client库。如果抛出此异常,我已经将调用包装在Polly网络策略中重试调用次数为3次,但Visual Studio坚持认为异常为“User-Unhandled”我需要修改哪些内容才能处理此异常? 我正在使用Visual Studio 2017社区版和C#4.6.1。在使用Polly时在VS调试器中报告异常用户未处理报告

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个地址进行处理。下一次运行请求将在一秒之内处理,但第一次不起作用。

回答

2

TL; DR您刚刚看到VS调试器违反例外。

您可了解Visual Studio调试器的(本质上混淆)的术语用户未处理的异常意味着执行代码库作为一个整体没有处理异常;不是这种情况。

用户未处理的异常在这种情况下意味着异常处理other than by your code - 在这里,由波利政策。

首先,Visual Studio将您正在调试的代码分为'my code' aka 'user code', and 'non-user code'。在你的场景中,Polly和Geocoder库将被分类为“非用户代码”。其次,默认情况下,Visual Studio调试器对由“非用户代码”处理的异常(但是使用这种可能令人困惑和警告的术语'user-un processed!')处理的异常中断[+]。

默认情况下,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) { ... },但调试器可能会再次误导性地标记最终异常'user-unhandled' - 即使您尝试捕获它 - 由于它最初被Polly捕获。

+0

Visual Studio调试器让我认为异常没有被正确处理。这是,波利政策多次正确地重复了这个呼叫。对我来说,这是由另一个问题进一步混淆,我的geocoderService对象没有完全重置之前再次调用。一旦我重新初始化geocoderService对象,一切正常。我也没有看到System.Diagnostics.Debug.WriteLine消息,因为我没有在库构建部分中选中定义的DEBUG常量。你的解释让我明白它应该!很好的解释! – user2197446