获取 System.InvalidOperationException:由于使用服务总线消息时对象的当前状态,操作无效

Getting System.InvalidOperationException: Operation is not valid due to the current state of the object while consuming Service Bus message

我在部署到 env 时收到以下异常堆栈跟踪,它在我的本地工作正常:

servicebus.windows.net/poc.enterprisebus.contracts.events/updatedevent/Subscriptions/poc.Test.Worker during Receive

servicebus.windows.net/poc.enterprisebus.contracts.events/updatedevent/Subscriptions/poc.Test.Worker during Receive
 [INFO] System.InvalidOperationException: Operation is not valid due to the current state of the object.
 [INFO]    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.OnReceiveAsync(Int32 maxMessageCount, TimeSpan serverWaitTime)
 [INFO]    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.<>c_DisplayClass64_0.<<ReceiveAsync>b_0>d.MoveNext()
 [INFO] — End of stack trace from previous location —
 [INFO]    at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
 [INFO]    at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
 [INFO]    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.ReceiveAsync(Int32 maxMessageCount, TimeSpan operationTimeout)
 [INFO]    at Microsoft.Azure.ServiceBus.Core.MessageReceiver.ReceiveAsync(TimeSpan operationTimeout)
 [INFO]    at Microsoft.Azure.ServiceBus.MessageReceivePump.<MessagePumpTaskAsync>b__11_0()

我的项目在 .Net5.0 中,我不想降级,除非没有 options.like 建议here 任何 suggestion/gudance 都将受到高度赞赏。

提前致谢。

由于您不想从 .Net 5.0 版本降级,您可以使用 Azure.Messaging.ServiceBus 客户端库而不是使用 Microsoft.Azure.ServiceBus.

尝试使用以下代码 receive/consume 使用 Azure.Messaging.ServiceBus 客户端库从服务总线发送消息:

// create a receiver that we can use to receive the message
ServiceBusReceiver receiver = client.CreateReceiver(queueName);

// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

// get the message body as a string
string body = receivedMessage.Body.ToString();
Console.WriteLine(body);

根据错误详细信息,我认为这是由于遗留服务总线包引用的 Microsoft.Azure.Amqp 版本中的错误所致。 (详情见#165

在您的项目中添加对 Microsoft.Azure.Amqp 包的直接引用应该可以提升版本并解决该问题。

值得注意的是,Microsoft.Azure.ServiceBus 包已被正式弃用,不建议用于新开发。在可预见的未来,它将继续受到支持,但将不再获得增强功能或 non-critical 修复。

如果可能,我们建议移动到 Azure.Messaging.ServiceBus