我有一个TransactionScope对象,我想将它用于使用Parallel.ForEach创建的所有任务,我该如何实现?
我想并行写入消息队列,20-50条消息,消息队列是事务性的:
using (var queue = new MessageQueue(_exportEndpoint))
{
var label = string.Format("{0} ComponentId - {1}", DateTime.Now.ToUniversalTime(), componentId);
queue.Send(contents, label, MessageQueueTransactionType.Automatic);
_log.WriteInfo("ExportToQueue: Message sent to queue - " + label);
}
主线程正在使用TransactionScope对象,我尝试了以下操作但是我在事务提交上有时间:
var clone = Transaction.Current.DependentClone(DependentCloneOption.RollbackIfNotComplete);
Parallel.ForEach(components.ToList(), c => ExportComponent(c, clone));
答案 0 :(得分:2)
排序!
我错过了DependentTransaction的Complete
答案 1 :(得分:0)
只要这些任务单独参与事务上下文,它就是透明的。换句话说,用事务范围包装你的foreach就完成了。