azure-sdk-for-net: System.InvalidOperationException: 'Local transactions cannot span multiple entities or partitions.

I’m trying to use the “send via” feature to be able to send messages to more than one entity inside a transaction.

I have the following entities:

  • Topic: mytesttopic, which has one subscription: input_subscription
  • Topic: output_1
  • Topic: output_2
  • Queue: transferqueue

I’m trying to receive sessions from input_subscription and for each received message:

  1. Send a new message to output_1 (via transferqueue)
  2. Send another message to output_2 (via transferqueue)
  3. Complete the received message

All within one transaction, meaning, if the transaction doesn’t complete, then the received message should be restored in the input_subscription and no messages should have been send.

Visually:

                                                 ┌⟶ output_1
input_subscription ⟶ Handler ⟶ transferqueue ─┤
                                                 └⟶ output_2

This is the code:

var incomingSubscriptionClient = new SubscriptionClient(connection, "mytesttopic", "input_subscription", ReceiveMode.PeekLock, new NoRetry());
var output1sender = new MessageSender(connection, "output_1", "transferqueue");
var output2sender = new MessageSender(connection, "output_2", "transferqueue");

incomingSubscriptionClient.RegisterSessionHandler(async (receivedMessage, message, cancellation) =>
{
	using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
	{
		await output1sender .SendAsync(new Message
		{
			Body = Encoding.UTF8.GetBytes("something"),
			SessionId = "mySessionId"
		});

		await output2sender .SendAsync(new Message
		{
			Body = Encoding.UTF8.GetBytes("something else"),
			SessionId = "mySessionId"
		});

		await session.CompleteAsync(receivedMessage.SystemProperties.LockToken);

		transaction.Complete();
	}
}, new SessionHandlerOptions(exception =>
{
	Console.WriteLine("session Receiver exception: " + exception.Exception.Message);
	return Task.CompletedTask;
}));

But I’m getting this exception when completing the received message:

System.InvalidOperationException: ‘Local transactions cannot span multiple entities or partitions.’

I’m not sure if this is a bug or if I’m doing something wrong.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@axisc Yes, that seems to work!! That’s awesome.

Thanks