WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Creating the Application
I'll now use Windows Workflow to create a new Sequential Workflow Console Application. This will allow me to demonstrate how transactions are implemented in Windows Workflow. Name the project TransactionWorkflow (see
Figure 7).

Figure 7. Create a new Sequential Workflow Console Application in Windows Workflow.
|
|

Figure 8. Design the workflow by adding the activities in proper sequence. |
Designing the Workflow
Populate Workflow1.vb with the activities shown in
Figure 8. These include a TransactionScope and three Code activities. You must add them to the form in the top-down sequence shown in
Figure 8. Set the (Name) properties of the three Code activities as "DebitAccount," "CreditAccount," and "TransactionCompleted," respectively.
Double-click on each Code activity to switch to the code-behind of the workflow. This will add and activate the event handler for each Code activity for you automatically.
The code-behind of each Code activity should look like this:
Public class Workflow1
Inherits SequentialWorkflowActivity
Private Sub DebitAccount_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
End Sub
Private Sub CreditAccount_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
End Sub
Private Sub TransactionCompleted_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
End Sub
End Class
As you can guess from the workflow, in this application you will debit an amount from one account and then credit the same amount into the other account. If any one of these events fails, the entire transaction rolls back.
Coding the Workflow
Now that you have designed the workflow, it's time to write the code to wire up all the activities. But first, add a connection string of the database to use in the Settings tab of the project property page. To do this, right-click on project name in Solution Explorer and select Properties (see Figure 9).
Name the setting "ConnectionString" and set its value as "Data Source=.\SQLEXPRESS;Initial Catalog=BankDatabase;Integrated Security=True;Pooling=False".
 | |
Figure 9. Add a new connection string as an application setting. |
In
Module1.vb, add the boldface portion of the following code snippet so that the workflow application can make use of the SqlWorkflowPersistenceService service:
Shared Sub Main()
Using workflowRuntime As New WorkflowRuntime()
AddHandler workflowRuntime.WorkflowCompleted, _
AddressOf OnWorkflowCompleted
AddHandler workflowRuntime.WorkflowTerminated, _
AddressOf OnWorkflowTerminated
workflowRuntime.AddService( _
New SqlWorkflowPersistenceService( _
"Initial Catalog=SqlPersistenceService;" & _
"Data Source=.\SQLEXPRESS;Integrated " & _
"Security=SSPI;"))
Dim workflowInstance As WorkflowInstance
workflowInstance = _
workflowRuntime.CreateWorkflow(GetType(Workflow1))
workflowInstance.Start()
WaitHandle.WaitOne()
Console.ReadLine()
End Using
End Sub
Back in the code-behind of Workflow1.vb, code DebitAccount as follows:
Private Sub DebitAccount_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim conn As New SqlConnection(My.Settings.ConnectionString)
Dim command As New SqlCommand("UPDATE Accounts SET Balance = Balance - 100 WHERE UserID='12345'", conn)
command.Connection.Open()
command.ExecuteNonQuery()
Console.WriteLine("---Account debited from 12345---")
conn.Close()
End Sub
Here, you debit $100 from account 12345.
Next, you will credit the $100 into account 54321. This is accomplished by the Code activity CreditAccount:
Private Sub CreditAccount_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim conn As New SqlConnection(My.Settings.ConnectionString)
Dim command As New SqlCommand("UPDATE Accounts SET Balance = Balance + 100 WHERE UserID='54321'", conn)
command.Connection.Open()
command.ExecuteNonQuery()
Console.WriteLine("---Account credited into 54321---")
conn.Close()
End Sub
The last Code activity (TransactionCompleted) simply prints out the balance of both accounts for 12345 and 54321:
Private Sub TransactionCompleted_ExecuteCode( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim conn As New SqlConnection(My.Settings.ConnectionString)
Dim command As New SqlCommand("SELECT Balance FROM Accounts WHERE UserID='12345'", conn)
command.Connection.Open()
Dim balance As Double = CDbl(command.ExecuteScalar())
Console.WriteLine("---Balance in account 12345 is $" & balance)
command.CommandText = "SELECT Balance FROM Accounts WHERE UserID='54321'"
balance = CDbl(command.ExecuteScalar())
Console.WriteLine("---Balance in account 54321 is $" & balance)
conn.Close()
End Sub