ContinueWith中的异常处理无限调用

时间:2022-12-21 02:17:11

I'm calling an async method (Specifically: Microsoft.ServiceBus.Messaging.QueueClient.SendAsync()) and want to be able to handle the exceptions within.

我正在调用异步方法(具体来说:Microsoft.ServiceBus.Messaging.QueueClient.SendAsync())并希望能够处理其中的异常。

One possibility I have come across is:

我遇到的一种可能性是:

_queueClient.SendAsync(message).ContinueWith((t) =>
{
    Debug.WriteLine("Exception: " + t.Exception.InnerException.GetType().Name);
}, TaskContinuationOptions.OnlyOnFaulted);

However the ContinueWith seems to get called infinitely:

然而,ContinueWith似乎被无限调用:

Exception: UnauthorizedAccessException
Exception: UnauthorizedAccessException
Exception: UnauthorizedAccessException
Exception: UnauthorizedAccessException
Exception: UnauthorizedAccessException
Exception: UnauthorizedAccessException
......................................
......etc

Any ideas why this happens and how to get around it?

任何想法为什么会发生这种情况以及如何解决它?

Thanks

谢谢

1 个解决方案

#1


0  

I was calling the send within a TraceListener which was then picking up the Debug.WriteLine and causing a loop.

我在TraceListener中调用send,然后拾取Debug.WriteLine并导致循环。

The implication is that a TraceListener listens for traces, but it seems it also listens to Debug messages as well.

这意味着TraceListener会侦听跟踪,但它似乎也会侦听Debug消息。

#1


0  

I was calling the send within a TraceListener which was then picking up the Debug.WriteLine and causing a loop.

我在TraceListener中调用send,然后拾取Debug.WriteLine并导致循环。

The implication is that a TraceListener listens for traces, but it seems it also listens to Debug messages as well.

这意味着TraceListener会侦听跟踪,但它似乎也会侦听Debug消息。