在Azure Service Bus SendAsync方法上捕获异常时遇到问题

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

我正在尝试使用以下代码来设置故障条件,即没有可用的网络路径,因此代码根本不能发送到服务总线。我知道这是因为我在测试时禁用了我的网络端口。

我仍然遇到代码的异步性质问题。我不知道在控制台应用程序中,我有如何附加一些会记录我知道应该生成的异常的东西。

我如何看到该异常文本?

        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;
        }

    }
c# azure azureservicebus
1个回答
0
投票

根据您的代码,我假设您使用的是Azure Service Bus .NET标准客户端库Microsoft.Azure.ServiceBus。根据我的测试,您可以利用以下代码捕获异常,如下所示:

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);
}

如果网络中断,您可以捕获异常,如下所示:

enter image description here

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