Home > OS >  Getting System.InvalidOperationException: Operation is not valid due to the current state of the obj
Getting System.InvalidOperationException: Operation is not valid due to the current state of the obj

Time:02-11

I am getting the below exception stacktrace while deployed to env, it's working fine in my local:

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()

My project is in .Net5.0 and downgrading is something I don't wanna do unless no options.like suggestedhere Any suggestion/gudance will be highly appretiated.

Thanks in advance.

CodePudding user response:

As you don't want downgrading from .Net 5.0 version, You can use the Azure.Messaging.ServiceBus client library instead of using Microsoft.Azure.ServiceBus.

Try the below code to receive/consume a message from service bus using the Azure.Messaging.ServiceBus client library:

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

CodePudding user response:

From the error details, I believe this is due to a bug in the version of Microsoft.Azure.Amqp that is referenced by the legacy Service Bus package. (details can be found in #165)

Adding a direct reference to the Microsoft.Azure.Amqp package in your project should hoist the version and work around the issue.

It is worth noting that the Microsoft.Azure.ServiceBus package has been officially deprecated and is not advised for new development. It will continue to be supported for the foreseeable future, but will no longer be receiving enhancements or non-critical fixes.

If possible, we recommend moving to Azure.Messaging.ServiceBus.

  • Related