Search code examples
azureazureservicebusazure-servicebus-queues

Returned Azure service bus queue sequence number different in my consumer than what was returned in the producer and shown in the Azure portal?


When I create a scheduled service bus message, both in Azure Portal and in my app using the Service bus producer code (below) and I receive a sequence number. I save it in my db.

Problem - When my Service bus consumer code is triggered by the dequeue of the scheduled message the sequence number is different than the one that was initially given to me by both the service bus producer code and through the Azure portal.

Shown here, where '13' is the sequnce number shown in Azure Portal screen.

enter image description here

Here is the code that receives the scheduled message and you can see the sequence number is different!

enter image description here

Here is my consumer code (don't think it matters)

    private async Task MessageHandler(ProcessMessageEventArgs args)
    {
        string body = args.Message.Body.ToString();
        JObject jsonObject = JObject.Parse(body);
        var eventStatus = (string)jsonObject["EventStatus"];

        await args.CompleteMessageAsync(args.Message);

        // fetch row here by sequence number
        // edit some data from entity, then save
        int result = await dbContext.SaveChangesAsync();

    }

Here is my producer code

    public async Task<long> SendMessage(string messageBody, DateTimeOffset scheduledEnqueueTimeUtc)
    {
        await using (ServiceBusClient client = new ServiceBusClient(_config["ServiceBus:Connection"]))
        {
            ServiceBusSender sender = client.CreateSender(_config["ServiceBus:Queue"]);

            ServiceBusMessage message = new ServiceBusMessage(messageBody);

            var sequenceNumber = await sender.ScheduleMessageAsync(message, scheduledEnqueueTimeUtc);
            
            return sequenceNumber;
        }
    }

Solution

  • From the documentation:

    The SequenceNumber for a scheduled message is only valid while the message is in this state. As the message transitions to the active state, the message is appended to the queue as if had been enqueued at the current instant, which includes assigning a new SequenceNumber.