0

I have an application that has a ServiceBusTrigger Azure Function. In the handler of this function, after I receive a queue message in my "QueueA" I need to do some business logic and, in the end, I have to do this:

  • publish a message to a "TopicA" that another systems will consume
  • publish another kind of message to my "QueueB", the "QueueB" will save the content to a database, updating the status of my event
  • move a file from one folder to another inside a file share storage
  • save the content of message received in "QueueA" to a database

I need to do all this stuff inside a Transaction Scope, because if one item fail, any of the other items can't go on.

My cenário was working just fine without the second item, thus after I tried to publish to a topic AND to a queue inside the same transaction I got this error message:

"Local transactions cannot span multiple top-level entities such as queue or topic."

From Microsoft's documentation I found this:

Service Bus supports grouping operations against a SINGLE messaging entity (queue, topic, subscription) within the scope of a transaction. For example, you can send several messages to one queue from within a transaction scope, and the messages will only be committed to the queue's log when the transaction successfully completes.

I'm just stuck here and don't know how can I do it and keep it thread safe. Anyone passed through it and can help me to post to a queue and a topic inside a transaction scope?

1 Answer 1

1

TL;DR: you can send/publish to multiple destinations with Azure Service Bus using cross-entity transaction API up to 100 messages.

Transactional processing, transaction scope, and Azure Service Bus cross-entity transaction are not quite the same despite the word "transaction".

I need to do all this stuff inside a Transaction Scope because if one item fails, any of the other items can't go on.

In Azure, there's no such thing as a transaction that will span multiple resources/services and rollback in case of a failure. Azure Service Bus has its own transaction, and it won't share it with Azure SQL database transactions because DTC is not available in Azure or any cloud for that matter.

What do you do? You break the work into isolated pieces that, if successful, emit a message to signal continuation (moving forward to the next task) or rollback (compensating action).

My cenário was working just fine without the second item, thus after I tried to publish to a topic AND to a queue inside the same transaction... Anyone passed through it and can help me post to a queue and a topic inside a transaction scope?

The documentation you've referred to, there's a code snippet that does exactly that:

var options = new ServiceBusClientOptions { EnableCrossEntityTransactions = true };
await using var client = new ServiceBusClient(connectionString, options);

ServiceBusReceiver receiverA = client.CreateReceiver("queueA");
ServiceBusSender senderB = client.CreateSender("queueB");

ServiceBusReceivedMessage receivedMessage = await receiverA.ReceiveMessageAsync();

using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    await receiverA.CompleteMessageAsync(receivedMessage);
    await senderB.SendMessageAsync(new ServiceBusMessage());
    ts.Complete();
}

Unfortunately, the sample doesn't mention that it's not enough to have a topic. A subscription under the topic is necessary as well. But that's not the problem you're having. Verify that sending and publishing work in isolation w/o any other scope as Service Bus doesn't work if it finds anything else enlisted in the transaction. It could be that the error message is misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.