您应该将您的流程设计为能够处理偶尔的断开连接和故障,因为这是一个“功能”或存在于云中的应用程序。
使用事务来管理代码的关键区域。
下面的伪/注释代码,以及指向 Microsoft 文档的链接在这里。
var msg = receiver.Receive();
using (scope = new TransactionScope())
{
// Do whatever work is required
// Starting with computation and business logic.
// Finishing with any persistence or new message generation,
// giving your application the best change of success.
// Keep in mind that all BrokeredMessage operations are enrolled in
// the transaction. They will all succeed or fail.
// If you have multiple data stores to update, you can use brokered messages
// to send new individual messages to do the operation on each store,
// giving eventual consistency.
msg.Complete(); // mark the message as done
scope.Complete(); // declare the transaction done
}