Partilhar via


Basic TransactionScope

This topic applies to Windows Workflow Foundation 4 (WF4).

This sample consists of four scenarios that run showing how to nest TransactionScope instances. The first scenario shows nesting a 3rd party activity of which the author has no knowledge of the construction. The second and third scenarios show how time-outs are respected and the final scenario shows the AbortInstanceOnTransactionFailure setting.

Nesting of TransactionScopeActivity

The workflow for the first scenario consists of a sequence of two WriteLine activities and a TransactionScope. The body of the TransactionScope is a sequence of two more WriteLine activities, a custom activity that prints the local identifier of the transaction and a third party activity. The third party activity TransactionScopeTest contains a TransactionScope although the workflow author has no way of knowing. This scenario shows that TransactionScope activities can be nested.

Time-Outs

The workflow for the second scenario is nearly identical to the first. The TransactionScopeTest has been replaced with a TransactionScope. The body of the TransactionScope is a five-second delay and the time-out for the transaction is set to two seconds. The time-out for the outer TransactionScope is set to 10 seconds. Note that the smallest time-out in scope is respected and the transaction times out.

The workflow for the third scenario is nearly identical to scenario two. The delay activity has been moved from the body of the inner TransactionScope to immediately after it in the sequence that is the body of the outer TransactionScope. The transaction still times out but because the two second time-out of the inner TransactionScope no longer applies. The transaction times out at 10 seconds when the outer TransactionScope time-out is exceeded.

Aborting on Transaction Failure

This workflow is similar to scenario three except the time-outs have been replaced by the AbortInstanceOnTransactionFailure property. Note that the flags of all inner children, if set, must match the outer TransactionScope. In this scenario, they do not and an exception is thrown when the workflow opens.

To run the sample

  1. Open the BasicTransactionScopeSample.sln solution in Visual Studio 2010.

  2. To build the solution, press CTRL+SHIFT+B or select Build Solution from the Build menu.

  3. Once the build has succeeded, press F5 or select Start Debugging from the Debug menu. Alternatively you can press CTRL+F5 or select Start Without Debugging from the Debug menu to run without debugging.

Dd807498.Important(en-us,VS.100).gif Note:
The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WF\Basic\Transactions\BasicTransactionScope