skip to Main Content

When I try to run a newly created Azure function locally and getting a message from a ServiceBus topic I get this error:

DeliveryCount = ‘mySbMsg.DeliveryCount’ threw an exception of type ‘System.InvalidOperationException’

This is the code I ran:
VS snippet

"ex" is not the values I am using, just changed for the snippet.

2

Answers


  1. Chosen as BEST ANSWER

    @Pravallika KV, Thank you so much for taking the time to reply. :)

    I tried your steps, but still got the same error. When I received the message as string it worked, but not with the ServiceBusRecievedMessage class.

    After I updated this NuGet it works with ServiceBusRecievedMessage class: Microsoft.Azure.WebJobs.Extensions.ServiceBus.


  2. DeliveryCount = ‘mySbMsg.DeliveryCount’ threw an exception of type ‘System.InvalidOperationException’

    As per my research, I found that this exception occurs when the Delivery Count property is not set properly.

    Check if the below troubleshooting steps help to fix the issue:

    • Make sure that, the message is not getting locked by any other receiver when triggering the Service Bus topic.
    • Make sure that, the message is not being received by the same receiver multiple times.
    • Also, Check if the multiple receivers are trying to receive the message.
    • Check if you are trying to receive the message after the lock got expired.
    • Try to increase the Delivery Count and check if you are facing the same issue.

    enter image description here

    Basically, InvalidOperationException occurs when the requested user operation isn’t allowed within the service. Check for the error details.


    I have created a ServiceBusTopicTrigger function to check and it worked as expected:

    Code Snippet:

    public class Function1
        {
            private readonly ILogger<Function1> _logger;
    
            public Function1(ILogger<Function1> log)
            {
                _logger = log;
            }
    
            [FunctionName("Function1")]
            public void Run([ServiceBusTrigger("mytopic", "mysubscription", Connection = "connection")]ServiceBusReceivedMessage message, ILogger log)
            {
               
                    log.LogInformation($"C# ServiceBus topic trigger function processed message: {message.Body}");
                
            }
        }
    

    Output:

    enter image description here

    References:

    • Refer doc to know about Service Bus exceptions.
    • Found a similar issue, refer to github.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search