I am trying to build a queue so that when I pull messages off the service bus, I can let them build up until I have a thousand before I send them to my database. I looked at ReceiveBatch but it only lets me grab up to 256 at most and the number that comes back is random. This forces me to make multiple calls and hope that I get around 1000. I need this batching for DB performance reasons.
I gave up on ReceiveBatch and tried to use OnMessage() instead to avoid incurring extra charges for multiple receivebatch calls and to get a more accurate batch size.
I've tried to create various client-side queue types ConcurrentQueue, ConcurrentBag, BlockingCollection to temporarily hold my OnMessage(m) received messages but no matter what I use the BrokerMessage is disposed when I try to access it outside of the OnMessage thread. I even tried creating a class level variable to keep a reference to it. I am pretty positive that OnMessage is auto disposing the message. I can't call the complete inside of OnMessage, so how am I supposed to work with the message. I need to stop it from auto-disposing.
var batchingQueue = new ConcurrentBag<BrokeredMessage>();
myQueueClient.OnMessage((m) =>
{
Console.WriteLine("Queueing message");
batchingQueue.Add(m);
});
while (true)
{
BrokeredMessage msg;
while (batchingQueue.TryTake(out msg))
{
...do this until I have a thousand ready to be written to DB in batch
Console.WriteLine("Completing message");
msg.Complete(); // <== BROKER MESSAGE DISPOSED ERROR HERE
}
}