0

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

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:

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

Henry Ing-Simmons
  • 1,362
  • 5
  • 18
  • 25
  • Any advice on solving this? – Henry Ing-Simmons Oct 08 '14 at 16:27
  • It doesn't make sense to me that it would be orphaned since the onlyonfaulted part of the continuewith should mean that it only runs if the parent task throws an exception. – Henry Ing-Simmons Oct 08 '14 at 16:30
  • If you're seeing this many times then it means that there's a call to `SendAsync` failing for each one. It's that simple. – Servy Oct 08 '14 at 16:35
  • 1
    Who/what calls _queueClient.SendAsync? Is it in a loop? Behind another service? Does it have an automatic retry? Consider logging something from the message to confirm that it is actually the same message each time. – Adam47 Oct 08 '14 at 17:52
  • We are missing the key piece of code which is the method calling `SendAsync` – Yuval Itzchakov Oct 08 '14 at 17:59
  • Ok so the send is being called within a TraceListener. And it would seem that the trace listener is picking up my Debug.WriteLines - hence the loop... – Henry Ing-Simmons Oct 09 '14 at 08:16

1 Answers1

0

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

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

Henry Ing-Simmons
  • 1,362
  • 5
  • 18
  • 25