Azure Service Bus with transaction scope

1.7k Views Asked by At

Namespace: Azure.Messaging.ServiceBus

I am using a transaction scope around my ServiceBusSender.SendMessageAsync operation and getting "Local transactions are not supported with other resource managers/DTC."

Can transaction scopes be used with Azure Service Bus?

Current scope:

using (var transaction = new TransactionScope(
   TransactionScopeOption.Required,
   new TransactionOptions
   {
      IsolationLevel = IsolationLevel.Serializable
   },
   TransactionScopeAsyncFlowOption.Enabled))
{
   try
   {
     // do a database operation
     // do azure bus sendmessageasync
   }
    catch
   {
      transaction.Dispose();
      throw;
   }
}
1

There are 1 best solutions below

0
Sean Feldman On

Cross entity transactions are possible with Azure.Messaging.ServiceBus (track 2) SDK.

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

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

ServiceBusReceivedMessage receivedMessage = await receiverA.ReceiveMessageAsync();

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