locked
Imperative Transaction blocks application execution RRS feed

  • Question

  • 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" }
                //        }
                //    },
                //},
             }
         };
    }
    


     

    Saturday, October 1, 2011 10:41 AM

Answers

  • Hi,

    "A workflow can have only a single ambient transaction for each workflow instance; nested transactions are not available. Even if the workflow contains a TransactionScope activity, this does not create a new inner transaction. Instead, this reuses the ambient transaction that was created outside the workflow. "

    Since the inner transaction is not available, the transaction was nested incorrectly in your project. Anyway, there should be a compile warning or friendly runtime error message. You can submit this issue in Microsoft connect site.

    • Marked as answer by LeoTang Monday, October 10, 2011 1:11 AM
    Thursday, October 6, 2011 2:29 PM