我正在使用Polly来捕获在调用普特尼鲍尔地理编码服务时的异常。我正在使用一个g1client库,它会抛出MessageProcessingException异常。我已经将该调用包装在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个地址进行处理。下一次运行将在不到一秒钟内处理该请求,但是第一次不起作用。