TransactionScope with nested transactions

Wednesday, July 9, 2014

using System; using System.Transactions;


namespace ConsoleApplication3 { public class Program { public static void Main(string[] args) { var trans = new TransactionScope(TransactionScopeOption.RequiresNew);



try
{
if (!ProcessMessage(true))
{
MoveMessageToErrorQueue();
}

trans.Complete();
}
catch (Exception)
{
trans.Dispose();
}
}

private static bool ProcessMessage(bool throwError)
{
//write to database

var trans = new TransactionScope(TransactionScopeOption.Required);

try
{
if (throwError)
throw new Exception();

trans.Complete();

return true;
}
catch (Exception)
{
trans.Dispose();
}

return false;
}

private static void MoveMessageToErrorQueue()
{
var trans = new TransactionScope(TransactionScopeOption.Required);

trans.Complete();
}
}


}


How can I make the transaction scope inside the MoveMessageToErrorQueue method enlist in the main transaction, not in the one inside the ProcessMessage method?


When it is trying to create a new transaction inside the MoveMessageToErrorQueue I get an exception stating that the transaction has already been aborted.


Thanks







http://ift.tt/1ojnWra