views:

38

answers:

2

I have a TransactionScope object and I want to use it for all the tasks created using Parallel.ForEach, how do I achieve this?

I want to writing to a message queue in parallel, 20-50 messages, message queue is transactional:

 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);
 }

And the main thread is using a TransactionScope object, I tried the following but I get a time out on the commit of the transaction:

var clone = Transaction.Current.DependentClone(DependentCloneOption.RollbackIfNotComplete);
Parallel.ForEach(components.ToList(), c => ExportComponent(c, clone));
A: 

As long as those tasks participate individually with the transaction context, it is transparent. In other words, wrap your foreach with the transaction scope and you are done.

Ken
The parallel task is attempting to write to a transactional message queue and I need the thread to be initialised with a transaction to achieve this
AWC
A: 

sorted!

I was missing a Complete for the DependentTransaction

AWC