12

I'm not sure what has changed but all of a sudden I get an "InvalidOperationException - Operation is not valid due to the current state of the object". My code has definitely worked previously and I can't remember changing anything.

I'm using Microsoft.Azure.ServiceBus 4.1.3

I tried some very basic code and still get the same error:

var bus = new QueueClient("Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=Manage;SharedAccessKey=xxx", "service-event");
await bus.SendAsync(new Message(Encoding.UTF8.GetBytes("{\"test\":\"hi\"}")));

Any ideas?

Edit stacktrace:

at Microsoft.Azure.ServiceBus.Core.MessageSender.<OnSendAsync>d__58.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.Azure.ServiceBus.RetryPolicy.<RunOperation>d__19.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Azure.ServiceBus.RetryPolicy.<RunOperation>d__19.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Azure.ServiceBus.Core.MessageSender.<SendAsync>d__45.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at TwitchEventAdapter.EventHandler.<SendEvent>d__2.MoveNext() in C:\...\EventHandler.cs:line 66
user3713080
  • 399
  • 4
  • 17
  • Can you please post the 'full' stacktrace of the exception. – krishg Sep 06 '20 at 21:10
  • I updated the post with a stacktrace @KrishnenduGhosh-MSFT – user3713080 Sep 06 '20 at 21:22
  • sorry I meant also the full exception (not only stacktrace) like detailed message and innerexception (if any). – krishg Sep 06 '20 at 21:30
  • There is no inner exception and "Operation is not valid due to the current state of the object." is the full message I'm afraid – user3713080 Sep 06 '20 at 21:47
  • I am afraid I could not reproduce with the code snippet you shared trying with different scenario. I might need to look at more holistic view of your application code. Also, did you check if the queue is healthy? You can test it by sending message via some different client like [Service Bus Explorer](https://github.com/paolosalvatori/ServiceBusExplorer) – krishg Sep 07 '20 at 14:55
  • I tried connecting with Service Bus Explorer and it sent/received messages just fine. I created a brand new project which has the same problem for me and pushed it here: https://github.com/Anton0123/ServiceBusErrorDemo – user3713080 Sep 07 '20 at 15:10
  • 1
    Solved it by changing the target framework to something other than .NET 5.0. Thank you for your help anyways @KrishnenduGhosh-MSFT – user3713080 Sep 07 '20 at 16:13
  • Glad about that. Yeah .net 5 is still in preview. So all the older SDKs might not be yet compatible. However if you use the newer SB SDK [Azure.Messaging.ServiceBus](https://www.nuget.org/packages/Azure.Messaging.ServiceBus) (which is also still in preview) with .net 5, you would not encounter this. – krishg Sep 07 '20 at 16:28
  • it still happens in the release version of net 5 as of today – cadaov Jul 20 '21 at 15:42

2 Answers2

31

If you're using .Net 5.0 RC2, then installing Microsoft.Azure.ServiceBus version 5.0.0 should solve the problem.

Sean Kearon
  • 10,987
  • 13
  • 77
  • 93
2

Changing project version from .NET 5.0 to .NET core 3.1 solved the problem.

user3713080
  • 399
  • 4
  • 17