具有事务范围的 Azure 服务总线
Posted
技术标签:
【中文标题】具有事务范围的 Azure 服务总线【英文标题】:Azure Service Bus with transaction scope 【发布时间】:2021-08-25 18:22:45 【问题描述】:命名空间:Azure.Messaging.ServiceBus
我在我的 ServiceBusSender.SendMessageAsync
操作周围使用事务范围,并得到“其他资源管理器/DTC 不支持本地事务。”
事务范围可以与 Azure 服务总线一起使用吗?
当前范围:
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】:使用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();
【讨论】:
以上是关于具有事务范围的 Azure 服务总线的主要内容,如果未能解决你的问题,请参考以下文章
Azure 服务总线:通过具有内置重试策略的消息泵接收到的瞬时错误(异常)。为啥?
Azure Messaging-ServiceBus Messaging消息队列技术系列8-服务总线配额