1
votes

I am trying to send a couple of messages to a service bus queue in a batch. I am doing it by using SendBatch method of QueueClient.

I am getting a FaultException during this operation:

Batching brokered messages with distinct SessionId, PartitionKey, or MessageId is not supported for an entity with partitioning and duplicate detection enabled.

The exception's message is quite self-explanatory, and I would try to fix it, but sometimes it works, no exceptions are thrown.

So sometimes it throws an exception, sometimes it works. I do not change any settings of queues and I do not change the code.

Any ideas why it can happen?

2
Can it be that when it succeeds, then actually the whole batch belongs to the same single session? - Mikhail Shilkov
And you can confirm that when it works, it's the same state of entities/messages? Are you sending in the context of an incoming message ("send-via")? - Sean Feldman
@Mikhail we do not use session explicitly, can it be used implicitly because we cache and reuse QueueClient? - Alex Sikilinda
@SeanFeldman not sure what you mean by the same state, but the messages we send differ only in domain-specific values, we do not assign any value to messageid or partitionkey or sessionid. And, no, we are not sending in the context of an incoming message. - Alex Sikilinda
@AlexSikilinda Ok... but is your queue partitioned and is duplicate detection enabled? - Mikhail Shilkov

2 Answers

1
votes

This was likely because your batch contained messages that would go into different partitions. A batch must contain messages for only one partition. In my case, my partition was the SessionId, so I first grouped my messages by SessionId, then batched by those groups and it worked great.

var bySessionId = messages.GroupBy(s => s.SessionId);
foreach (var sessionSet in bySessionId)
{
    var msgBatch = sessionSet.Batch(20); //Using .Batch from MoreLinq
    foreach (var b in msgBatch)
    {
        var sbb = await sender.CreateMessageBatchAsync();
        b.ForEach(m => sbb.TryAddMessage(m));

        await sender.SendMessagesAsync(sbb);
    }
}
1
votes

I had same problem and was helped by this post: https://github.com/Azure/azure-service-bus/issues/103

what I realize is that the exception message is actually incorrect. It gives the impression that “distinct … MessageId is not supported with … duplicate detection enabled” when in fact it is required!

so after I create a message: var message = new Message(Encoding.UTF8.GetBytes(<..JsonString...>));

I added this line of code and it worked:

message.MessageId = Guid.NewGuid().ToString();