Catching exception in TransactionScope bug - exception never caught
I modified ImperativeTransactionSample of WF samples from WF_WCF_Samples and created situation when application execution is blocked. Here is code sample where Console.WriteLine("Stop") line is never executed:
using System;
using System.Activities;
using System.Activities.Statements;
using System.Transactions;
namespace Microsoft.Samples.ImperativeTransactionSample
{
sealed class Program
{
static void Main()
{
Console.WriteLine("Start");
try
{
using (System.Transactions.TransactionScope txScope = new System.Transactions.TransactionScope())
{
new WorkflowInvoker(BuildWF()).Invoke();
txScope.Complete();
};
}
catch (Exception ex)
{
Console.WriteLine("Exception : " + ex.Message);
}
Console.WriteLine("Stop");
Console.ReadLine();
}
static Activity BuildWF()
{
return new Sequence
{
Activities =
{
new TestActivity(),
new System.Activities.Statements.TransactionScope
{
Body = new Sequence
{
Activities =
{
new WriteLine { Text = "Hello" }
}
},
},
}
};
}
class TestActivity : NativeActivity
{
protected override void Execute(NativeActivityContext context)
{
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
throw new Exception("Test");
}
}
}
}
}
The strange thing is that if I remove creation of TransactionScope activity inside workflow in BuildWF method everything works fine. What's the problem here and how I can avoid this situation?
static Activity BuildWF()
{
return new Sequence
{
Activities =
{
new TestActivity(),
//new System.Activities.Statements.TransactionScope
//{
// Body = new Sequence
// {
// Activities =
// {
// new WriteLine { Text = "Hello" }
// }
// },
//},
}
};
}