Azure ServiceBus FaultTolerantAmqpObject`1' 异常
Azure ServiceBus FaultTolerantAmqpObject`1' Exception
因此,由于需要 TLS 1.2,我已升级到 Azure.Messaging.ServiceBus 版本 7.1.2,并将我的项目升级到 .NET 4.7.2。我像这样重写了监听器(这是一个预置 windows 服务)BusListener.cs
:
public static async Task ReceiveMessagesAsync()
{
await using (ServiceBusClient client = new ServiceBusClient(ConfigurationManager.AppSettings[SERVICE_BUS_CONNECTION_STRING]))
{
ServiceBusProcessor processor = client.CreateProcessor(QUEUE_NAME, new ServiceBusProcessorOptions());
processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;
await processor.StartProcessingAsync();
}
}
我调用 windows 服务 ServiceMain.cs
Task.Run(() => BusListener.ReceiveMessagesAsync());
我在等待大约 10 秒后收到此错误,但从未收到任何消息:
Cannot access a disposed object. Object name: 'FaultTolerantAmqpObject`1'. - at Microsoft.Azure.Amqp.Singleton`1.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Azure.Messaging.ServiceBus.Amqp.AmqpConnectionScope.d__54.MoveNext()
我环顾四周,从另一个答案看来是这样的:Service Bus Disposed Object that I could be running into issues due to the method being static, even though this is what I'm following in the Microsoft "how to" guide here: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues
我不确定是我写的服务总线部分不正确还是我遗漏了什么?有没有办法通过 ping 或测试连接来更好地了解问题所在?
当您创建 ServiceBusClient
时,它拥有与它创建的任何子对象所使用的服务的连接。通过在这一行中配置客户端:
await using (ServiceBusClient client = new ServiceBusClient(ConfigurationManager.AppSettings[SERVICE_BUS_CONNECTION_STRING]))
您正在关闭您创建的处理器使用的连接。我建议将客户端提升到方法之外,并允许它在应用程序的生命周期内作为单例存在。一般来说,这是我们推荐的管理 Azure 客户端的方法。 (参见:client lifetime)
因此,由于需要 TLS 1.2,我已升级到 Azure.Messaging.ServiceBus 版本 7.1.2,并将我的项目升级到 .NET 4.7.2。我像这样重写了监听器(这是一个预置 windows 服务)BusListener.cs
:
public static async Task ReceiveMessagesAsync()
{
await using (ServiceBusClient client = new ServiceBusClient(ConfigurationManager.AppSettings[SERVICE_BUS_CONNECTION_STRING]))
{
ServiceBusProcessor processor = client.CreateProcessor(QUEUE_NAME, new ServiceBusProcessorOptions());
processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;
await processor.StartProcessingAsync();
}
}
我调用 windows 服务 ServiceMain.cs
Task.Run(() => BusListener.ReceiveMessagesAsync());
我在等待大约 10 秒后收到此错误,但从未收到任何消息:
Cannot access a disposed object. Object name: 'FaultTolerantAmqpObject`1'. - at Microsoft.Azure.Amqp.Singleton`1.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Azure.Messaging.ServiceBus.Amqp.AmqpConnectionScope.d__54.MoveNext()
我环顾四周,从另一个答案看来是这样的:Service Bus Disposed Object that I could be running into issues due to the method being static, even though this is what I'm following in the Microsoft "how to" guide here: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues
我不确定是我写的服务总线部分不正确还是我遗漏了什么?有没有办法通过 ping 或测试连接来更好地了解问题所在?
当您创建 ServiceBusClient
时,它拥有与它创建的任何子对象所使用的服务的连接。通过在这一行中配置客户端:
await using (ServiceBusClient client = new ServiceBusClient(ConfigurationManager.AppSettings[SERVICE_BUS_CONNECTION_STRING]))
您正在关闭您创建的处理器使用的连接。我建议将客户端提升到方法之外,并允许它在应用程序的生命周期内作为单例存在。一般来说,这是我们推荐的管理 Azure 客户端的方法。 (参见:client lifetime)