Using WorkflowCommitWorkBatch Service Sample
This sample demonstrates how Windows Workflow Foundation enables custom services to participate in the same transaction as the workflow instance where the services are used.
Any composite activity that has transactional behavior associated with it also has a work batch associated with it. If any activities that are contained in this composite activity call a method on a service that is provided by the host, that service can also be a part of the work batch. When the composite activity is completed, the work batch is committed. If, for some reason, any item fails to commit, the entire work batch rolls back. As part of the commit process, the transaction context is provided to all the items in the batch. Therefore the service that wants to participate in the transaction also gets the transaction context. It can then pass along that transaction context to any calls it makes. For example, the most common thing is to pass the transaction context to a database call so that even the database calls take part in the same transaction.
Participating in the Work Batch
To participate in the work batch, the service must implement an interface called IPendingWork.
Registering with the Work Batch
To participate in the work batch, the service (or any type that has to participate in the work batch) must register with the work batch. The following scenario explains the steps that are required to register with the work batch.
During execution, an activity calls a method on the service. Instead of doing the actual work in the method, the method just registers with the work batch and the context that is required to finish the work. Later, when the work batch is to be committed, the Commit method is called with the context that was used at the time of registration. In other words, the method just defers the processing until it is appropriate to do the actual work.
To register with the work batch, the work item is added to the current work batch by using the following:
WorkflowEnvironment.WorkBatch.Add(IPendingWork work, object workItem);
The sample is an amount transfer scenario. A certain amount of money must be transferred from a savings account to a checking account. The workflow first credits the money in the checking account. It then tries to debit the money from the savings account. If the amount of money in the savings account is less than the amount to be transferred, the transaction aborts and the checking account is restored. If the amount is larger, the transaction succeeds.
As part of the commit method, the transaction context is passed to the database calls so that even the database participates in the same transaction.
The sample has three custom activities.
CreditAmount: Used to credit the amount into the checking account.
DebitAmount: Used to debit amount from the savings account.
TransactionComposite: A composite activity that inherits from the SequenceActivity activity. It provides the transaction behavior.
To create and configure the SQL Server database
Using Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named SqlPersistenceService by using the SQL query statement:
CREATE DATABASE SqlPersistenceService
Note When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended to use a single database for both persistence and tracking.
In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Schema.sql, where <framework_version> is the current version of the .NET Framework installed on your computer.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Logic.sql, where <framework_version> is the current version of the .NET Framework installed on your computer.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.
To build and run the sample
Download the sample by clicking Download Sample.
This extracts the sample project to your local hard disk.
Create the TransactionServiceSampleDB database by running the script for the database provided in the file .\SQL\TransactionServiceDB.cmd.
Note: This script creates the database named TransactionServiceSampleDB. The connection string that is used in the sample uses this database name. Note: Make sure that you update the correct connection string in the source code if you change the name of the database. Otherwise, the sample cannot connect to the database. Also, before running the script, verify that the correct location for your SQL server installation is specified in line 20 of the script.
Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.
Go to the source directory of the sample.
At the command prompt, type MSBUILD <Solution file name>.
In the SDK command prompt, run the .exe file in the Host\bin\debug folder (or the Host\bin folder for the Visual Basic version of the sample), which is located below the source folder of the sample.
To debug the sample, make the SampleWorkflow project the startup project by right-clicking the project in Solution Explorer and selecting Set as StartUp Project.
Open the Properties dialog box of the SampleWorkflow project.
Select Start an external program, and select the host.exe file in the Host\bin\Debug folder.
When you execute the Host.exe file, the sample expects you to enter an amount to be transferred from the savings account to the checking account. Enter a nonzero positive amount. The transaction succeeds or fails depending on the balances in the savings account. You will see an appropriate display on the screen.
© 2007 Microsoft Corporation. All rights reserved.