异常用户未处理报道VS调试器使用时,波利

问题描述 投票:2回答:1

我使用波莉捕捉异常而调用Pittney宝地理编码服务。我使用的是抛出一个MessageProcessingException一个g1client库。我裹在波利网络策略调用重试通话达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个地址进行处理。下一次运行该请求将第二下处理的,但第一次这是行不通的。

c# networking geocoding unhandled-exception polly
1个回答
7
投票

TL; DR你只是看到与调试打破例外。

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

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

首先,Visual Studio的划分正在调试到'my code' aka 'user code', and 'non-user code'代码。在方案中,波利和地理编码库将被归入“非用户代码”。

其次,默认情况下,Visual Studio调试器符[+]由“非用户代码”处理的异常(但使用这种潜在的混乱和令人震惊的术语“用户未处理”!)。

Visual Studio的休息[+]默认情况下,这样就可以看到触发例外,因为它发生线路(即使后面的代码将处理它...)。所以,它的声音(在调试器设置dpdg),如果你看到的Visual Studio破,即使是配置由波利政策的异常将被处理。只需按下F5 /在调试器中继续,和随后的代码将恢复。

您还可以通过以下的instructions in this article under the heading Tell the debugger to continue on user-unhandled exceptions改变这种行为[+]。

你可以进一步满足自己的波利政策是通过检查线路System.Diagnostics.Debug.WriteLine("Exception being retried" + exception);的行为工作。您应该看到从线路输出 - 或者看它打,如果你设置就可以了断点 - 如果正在调用重试。


最后,波莉重试政策rethrows any final exception if all configured retries have been exhausted。这是故意的 - 表示不测 - 而不是失败。

您正确地对此有一个try { ... } catch (MessageProcessingException ex) { ... },但调试器可能会再次误导性标签,其最终的例外“用户未处理” - 即使你有一个try-catch它 - 因为它最初被抓波利。

© www.soinside.com 2019 - 2024. All rights reserved.