Exercise 3: The CodeActivity

As you have seen so far, WF4 consists of a designer that edits .xaml files and a runtimethat invokes activities. When you author a workflow, you are creating a new kind of activity and because activities are just classes that inherit from System.Activities.Activity or one of its subclasses, you can declare workflows using C#, VB or XAML. In this exercise, you will implement your “greeting” business process by creating an activity in C# or VB.

As previously said, activities implement a business process. Some activities will implement the process by invoking other activities. For example, your SayHello activity did not actually write text to the console but instead used the WriteLine activity to do the work. could have implemented the same SayHello activity in C# or VB by inheriting from System.Activities.Activity and creating an Implementation as you see in the following examples

C#

public sealed class SayHelloActivity : Activity { WriteLine writeLine = new WriteLine() { Text = "Hello Workflow 4" }; public SayHelloActivity() { Implementation = () => { return writeLine; }; } }

Visual Basic

Public NotInheritable Class SayHelloActivity Inherits Activity Private writeLine As New WriteLine With {.Text = "Hello Workflow 4"} Public Sub New() Implementation = Function() Return writeLine End Function End Sub End Class

This approach is useful if you are creating a workflow out of other activities that do the work as did in exercise 1 with the WriteLine activity. However, there may be times when you want to create an activity that implements the business logic in itself or by calling another class that is not an activity to do the work. To do this, you will inherit from a different base class System.Activities.CodeActivity and override the Execute method.

Task 0 – Opening the Solution

To begin this exercise you can use the solution you finished from Exercise 2. Alternatively, you can follow the following steps to begin with Exercise 3.

  1. Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.
  2. Open the starting solution for Exercise 3 located under the Source\Ex3-CodeActivity\Begin folder and use it as the starting point for this exercise.
  3. Press CTRL+SHIFT+B to build the solution.

Task 1 – Creating the SayHelloInCode Activity

In this task, you will create an activity in code and write the text to the console using Console.WriteLine

  1. Right-click the HelloWorkflow project, select Add / New Item. From the Workflow templates select Code Activity and name the file SayHelloInCode.
  2. Delete the Text property from the template – you will not need it.
  3. CodeActivity is an abstract class. That means that when you inherit from it, you must override the Execute method. This is the place where you will do the work of your activity. Replace the default implementation of the class with the following code.

    (Code Snippet - Introduction to WF4Lab - SayHelloInCode Class CSharp)

    C#

    public sealed class SayHelloInCode : CodeActivity { protected override void Execute(CodeActivityContext context) { Console.WriteLine("Hello Workflow 4 in code"); } }

    (Code Snippet - Introduction to WF4Lab - SayHelloInCode Class VB)

    Visual Basic

    Public NotInheritable Class SayHelloInCode Inherits CodeActivity Protected Overrides Sub Execute(ByVal context As CodeActivityContext) Console.WriteLine("Hello Workflow 4 in code") End Sub End Class

Task 2 – Updating Main to Invoke SayHelloInCode

  1. Change Program.cs (C#) or Module1.vb (Visual Basic) to use the new SayHelloInCode class. To do this, locate the WorkflowInvoker.Invoke method code, and replace it with the following code:

    (Code Snippet - Introduction to WF4 Lab - InvokeSayHelloInCode Class CSharp)

    C#

    static void Main(string[] args)
    FakePre-24b8f84f73684f9886cae4be77b703a3-f6814fae9d9343f9a493f7c9790e43e8WorkflowInvoker.Invoke(new SayHelloInCode());FakePre-c0fd3f3652514e4babc40ba326b406e5-b7f7300ee19945e1b8c798513080eaff

    (Code Snippet - Introduction to WF4 Lab - InvokeSayHelloInCode Class VB)

    Visual Basic

    Shared Sub Main(ByVal args() As String)
    WorkflowInvoker.Invoke(New SayHelloInCode()) FakePre-c4fdd1197f1f4c75bbc3a833db21f4e6-97ed186c8ec849668b94b5d150b7d355

Next Step

Exercise 3: Verification