1

I'm trying to use the following code to setup a failure condition, namely were there is no network path available, so the code shouldn't be able to send to the Service bus at all. I know this because I disable my network ports when I test.

I am still having trouble with the Async nature of the code though. I don't know in a console application like I have how to attach something that would log out the exception that I know should be generated.

How do I see that exception text?

        public async Task TestQueueExists()
    {    
        _queueClient = new QueueClient(AppSettings.McasServiceBusConnectionString,
            AppSettings.ListServSyncQueueName);
        Logger.Information(
            $"Queue Created to: {_queueClient.QueueName} with RecieveMode: {_queueClient.ReceiveMode}");
        try
        {
            await _queueClient.SendAsync(new Message("Test".ToUtf8Bytes()));       
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

    }
Paul Duer
  • 1,100
  • 1
  • 13
  • 32

2 Answers2

1

The issue I ran into was that I was using a Console application and the Console app runs sync out of the box. I had to modify my Main to return a Task and use async for the exception to be caught, which makes sense.

ie

internal class Program
{
    static async Task Main(string[] args)
    {
         [..]
    }
}
Matt M
  • 1,093
  • 2
  • 11
  • 26
0

According to your code, I assumed that you are using the Azure Service Bus .NET Standard client library Microsoft.Azure.ServiceBus. Per my test, you could leverage the following code to capture the exception as follows:

try
{
    await _queueClient
        .SendAsync(new Message(Encoding.UTF8.GetBytes("hello world")))
        .ContinueWith(t =>
        {
            Console.WriteLine(t.Status + "," + t.IsFaulted + "," + t.Exception.InnerException);
        }, TaskContinuationOptions.OnlyOnFaulted);
    Console.WriteLine("Done");
}
catch (Exception e)
{
    Console.WriteLine(e);
}

If the network is break, you may capture the exception as follows:

enter image description here

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • It worked but I just wonder if the statement within ContinueWith will be executed or not if the user closes the application immediately after SendAsync? Usually SendAsync will take a little bit of time to fail. – Quan Mar 16 '19 at 04:58
  • This did not work for me. Console app is quitting with status 0. try/catch isn't catching anything; this ContinueWith approach isn't catching anything. something is nuking the app and nuking it HARD – Matt M Jun 08 '22 at 16:36