Azure Service Bus SendAsync方法上的异常捕获异常 [英] Trouble catching exception on Azure Service Bus SendAsync method
问题描述
我正在尝试使用以下代码来设置故障条件,即是否没有可用的网络路径,因此该代码根本不能发送到服务总线.我知道这一点是因为我在测试时会禁用网络端口.
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;
}
}
推荐答案
根据您的代码,我假设您正在使用Azure Service Bus .NET Standard客户端库
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:
这篇关于Azure Service Bus SendAsync方法上的异常捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!