Exercise 2: Refactoring Workflow

Even though you have a working application, you might want to make a few improvements. In this exercise, as Workflow1 is not a very descriptive name, you will change it to SayHello and see how the changes to the workflow name affect the rest of the program.

Task 0 – Opening the Solution

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

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

Task 1 – Renaming Workflow1 to SayHello

  1. To change the name of the workflow defined in Workflow1.xaml, Open it with the workflow designer and click on an empty area the designer surface. This will allow you to change the properties of the workflow.

    Figure 6

    Click on an empty area of the designer surface so you can set the name of the workflow

  2. In the Properties window, set the Name property of the Workflow to HelloWorkflow.SayHello (C#) or SayHello (VB).

    Figure 7

    Set the Name property of the workflow (C#)

    Figure 8

    Set the Name property of the workflow (VB)

  3. While it is not required, it is a good idea for the name of the .xaml file to match the name of the activity contained in it. In Solution Explorer, right-click Workflow1.xaml, select Rename and type SayHello.xaml.

    Note:
    Watch Out

    When you rename C# or Visual Basic files, Visual Studio asks you if you want refactoring to change the name of the class also. It does not do this when renaming XAML files, so if you want to change the name of the workflow also you need to do this manually.

  4. Press CTRL+SHIFT+B to build the solution. It will fail to compile because you changed the name of the workflow.

    Figure 9

    The workflow fails to compile (C#)

    Figure 10

    The workflow fails to compile (VB)

    Note:
    Why does the application fail to compile when I change the workflow name?

    Your workflow is actually a class declared in XAML that inherits from System.Activities.Activity. The name of the workflow is the name of the class. When you set the Name property, you changed the name of the class.

Task 2 – Updating the Main Method to Use the New Name

WF4 runs a workflow with the Workflow Runtime. The easiest way to invoke the runtime is to use the WorkflowInvoker class which is what the Workflow Console Application template uses.

  1. To fix the build break, open Program.cs (C#) or Module1.vb (Visual Basic) and modify it to use the new SayHello class name. To do this, locate the WorkflowInvoker.Invoke method call, and modify it to use SayHello instead of Workflow1 as shown in the following code (shown in bold).

    C#

    static void Main(string[] args) WorkflowInvoker.Invoke(new SayHello());
    {
    static void Main(string[] args) WorkflowInvoker.Invoke(new SayHello());FakePre-c5ab80e33d334bb8882032a9e834033a-e9b7c02d3d95472588d30ac08ad0cd22

    Visual Basic

    Shared Sub Main()
    WorkflowInvoker.Invoke(New SayHello())FakePre-d56000369bd3491b9b68610bd74825e2-93d0a2249d64426585e91bddd74fcc67

    Note:
    Why does Visual Studio warn me that the type SayHello is not defined?

    If you change Main() before compiling the workflow with the new SayHello name, you may notice a warning about the type SayHello not being defined. The reason for this is that Visual Studio is not aware of types declared in XAML until you build the solution.

Next Step

Exercise 2: Verification