-2

I'm reading messages from Azure Service Bus in my C# application. I'm reading them from Dead Letter Queue but I suppose it doesn't matter here. I need to read a block of messages of a given size, starting at a given offset (aka a page of messages).

I came up with the following, very inefficient, code:

SubscriptionClient client  = SubscriptionClient.CreateFromConnectionString(
  connectionString, 
  topic, 
  QueueClient.FormatDeadLetterPath(subscription));

var result = new List<string>();
for (var i = 0; i < offset + size; i++)
{
  var msg = await client.PeekAsync();
  if (msg == null)
  {
    return result;
  }

  if (i >= offset)
  {
    result.Add(msg);
  }
}

return result;

Is there a way I can write this "seek" in a more efficient way?

Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
  • 1
    There's no concept of offset. It's a queue, you can receive or peek. https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview – Sean Feldman Sep 25 '18 at 16:28

1 Answers1

0

The SubscriptionClient.ReceiveAsync method accepts an Int64 sequence number (offset):

for (var i = offset; i < offset + size; i++)
{
    var msg = await client.ReceiveAsync(i);
    result.Add(msg);
}
mm8
  • 163,881
  • 10
  • 57
  • 88