DOCX

Introduction To Windows Workflow Foundation in .NET 4

By Bill Andrews,2014-09-21 13:47
9 views 0
Introduction To Windows Workflow Foundation in .NET 4

Hands-On Lab

    Introduction To Windows Workflow

    Foundation in .NET 4

Lab version: 1.0.0

    Last updated: 9/21/2011

Contents

    OVERVIEW ............................................................................................................................................. 5

    EXERCISE 1: HELLO WORKFLOW ....................................................................................................... 7

    Task 1 Creating a Simple Hello Workflow Application .................................................................... 7 Exercise 1 Verification 10

    EXERCISE 2: REFACTORING WORKFLOW ....................................................................................... 11

    Task 0 Opening the Solution ........................................................................................................ 11

    Task 1 Renaming Workflow1 to SayHello..................................................................................... 11

    Task 2 Updating the Main Method to Use the New Name ........................................................... 14 Exercise 2: Verification 14

    EXERCISE 3: THE CODEACTIVITY ..................................................................................................... 15

    Task 0 Opening the Solution ........................................................................................................ 16

    Task 1 Creating the SayHelloInCode Activity ................................................................................ 16

    Task 2 Updating Main to Invoke SayHelloInCode ......................................................................... 17 Exercise 3: Verification 18

    EXERCISE 4: DYNAMIC WORKFLOWS WITH XAML .......................................................................... 18

    Task 0 Opening the Solution ........................................................................................................ 19

    Task 1 Modifying SayHello.xaml Files Properties ......................................................................... 19

    Task 2 Modifying Main() to Load the SayHello.xaml File .............................................................. 19 Exercise 4: Verification 21

    EXERCISE 5: TESTING WORKFLOWS ............................................................................................... 23

    Task 0 Opening the Solution ........................................................................................................ 23

    Task 1 Creating the Unit Test Project ........................................................................................... 23

    Task 2 Creating the Test .............................................................................................................. 25

    Task 3 Getting the Application to Compile ................................................................................... 26

    Task 4 Seeing the Test Fail ........................................................................................................... 28

    Task 5 Making the Test Pass ........................................................................................................ 28

    Exercise 5: Verification 30

    EXERCISE 6: WORKFLOWAPPLICATION .......................................................................................... 31

    Task 0 Opening the Solution ........................................................................................................ 31

Task 1 Writing the Test to Verify that the Workflow Thread ID is Returned as an Out Argument . 31

    Task 2 Returning the WorkflowThread as an Argument ............................................................... 33 Task 3 Modifying the Test to use WorkflowApplication ............................................................... 37 Exercise 6: Verification 40

    EXERCISE 7: IF/ELSE LOGIC .............................................................................................................. 41

    Task 0 Opening the Solution ........................................................................................................ 41

    Task 1 Writing the Test for a New Requirement .......................................................................... 42 Task 2 Implementing the New Requirement in the Workflow ...................................................... 43 Exercise 7: Verification 48

    EXERCISE 8: ERROR HANDLING ....................................................................................................... 48

    Task 0 Opening the Solution ........................................................................................................ 48

    Task 1 Writing a Test to Observe Error Behavior .......................................................................... 49 Task 2 Adding the Try/Catch Activity to the Workflow ................................................................. 50 Exercise 8: Verification 54

    EXERCISE 9: CUSTOM ACTIVITIES AND DESIGNERS ...................................................................... 54

    Task 0 Opening the Solution ........................................................................................................ 55

    Task 1 Add Activity Designer Library ............................................................................................ 55 Task 2 Create Composite Activity in XAML ................................................................................... 56 Task 3 Add Existing Activities ....................................................................................................... 58

    Task 4 Observe Custom Activities without Designers ................................................................... 59 Task 5 Explore Activity Designers ................................................................................................ 61

    Task 6 Linking the Activity Designers to the Activity .................................................................... 64 Exercise 9: Verification 65

    Task 1 Prepare the test projects .................................................................................................. 65

    Task 2 Create DiagnosticTrace Test ............................................................................................. 66 Task 3 Add a DiagnosticTrace activity to the SayHello Workflow ................................................. 68 Task 4 Add the PrePostSequence Test ......................................................................................... 69 EXERCISE 10: HOSTED DESIGNER ................................................................................................... 73

    Task 0 Opening the Solution ........................................................................................................ 74

    Task 1 Adding new WPF Application ............................................................................................ 74 Exercise 10: Verification 81

SUMMARY ............................................................................................................................................ 82

Overview

    Welcome to Windows Workflow Foundation 4 (WF) Microsoft's programming model, engine and tools for quickly building workflow-enabled applications. This release of WF in .NET Framework 4 changes several development paradigms from previous versions. Workflows are now easier to create, execute, and maintain.

    In this lab, you will get to know the basics of creating, hosting and running a workflow. This lab is also an introduction to the new workflow authoring constructs in the .NET Framework 4 and Visual Studio 2010, including the new Workflow Designer, expressions, variables and arguments. Additionally, you will explore the use of some basic built-in activities, create custom activities, activity designers and even re-host the workflow designer in a WPF application.

     Do I have to do all the exercises?

    This lab is long. To do all of it will probably take close to 2 hours. This lab has been designed so that each exercise is independent of the others. Just open the solution found under the Begin folder for the exercise. For example, if you wanted to learn how to create a custom activity designer you would start with Exercise 9 and open the solution from the Source\Ex9-ActivityDesigner\Begin folder

Objectives

    In this Hands-On Lab, you will learn:

    ; How to create a sequential workflow using the designer and XAML or, alternatively, using plain

    C# or Visual Basic code

    ; How to run and test the sequential workflow using the WorkflowApplication and

    WorkflowInvoker classes

    ; How to pass InArguments and receive OutArguments from a workflow

    ; How Expressions and Variables are used

    ; How to use the WriteLine, If, TryCatch, Catch and Throw activities

    ; How to load and run an activity from a xaml file

    ; How to create an activity designer

    ; How to host the WorkflowDesigner in your own application

System Requirements

    You must have the following items to complete this lab:

    ; Microsoft Visual Studio 2010

    ; Microsoft .NET Framework 4

Setup

    All the requisites for this lab are verified using the Dependency Checker. To make sure that everything is correctly configured, follow these steps:

    1. Run the Configuration Wizard for the Training Kit if you have not done it previously. To do this,

    browse to Source\Setup folder of this lab, and double-click the Dependencies.dep file. Install

    any pre-requisites that are missing (rescanning if necessary) and complete the wizard.

    Note: The Configuration Wizard is used for checking dependencies and setting up the

    environment. If the Configuration Wizard is not installed on your machine, you must install it

    running the DependencyChecker.msi file located on

    the %VS2010TKInstallationFolder%\Assets\DependencyChecker folder (e.g. by default the

    Training Kit is installed under C:\VS2010TrainingKit).

    For convenience, much of the code you will be managing along this hands-on lab is available as

    Visual Studio code snippets.

    To use code snippets with XAML, right-click on the xaml file, select Open With and choose

    XML Editor. Then you can insert XML snippets into the file.

Exercises

    This Hands-On Lab comprises the following exercises:

    Note: each exercise builds on the one before it but you can start at any point in the lab by opening the solution in the Begin folder for the exercise.

    1. Hello Workflow

    2. Refactoring Workflow

    3. The CodeActivity

    4. Dynamic Workflows with XAML

    5. Testing Workflows

    6. WorkflowApplication

    7. Adding If/Else Logic

    8. Error Handling

    9. Custom Activities and Designers

    10. Hosted Designer

Estimated time to complete this lab: 120 minutes.

    Starting Materials

    ; Visual Studio solutions. Depending on the exercise you will find Visual Studio solutions for C#

    and Visual Basic that you can use as starting point for the exercises.

     What if I get stuck?

    The source code that accompanies this hands-on lab includes an end folder where you can find a Visual Studio solution with the code that you would obtain if you complete the steps in each exercise. You can use this solution as a guide if you need additional help working through the exercises. Remember to build the solutions before opening any file using the workflow designer.

Next Step

    Exercise 1: Hello Workflow

Exercise 1: Hello Workflow

    Workflows execute a business process. Each step in that process is implemented by an Activity.

    In this exercise, you will create and test a simple “Hello World” process using Windows Workflow

    Foundation 4.

    Task 1 Creating a Simple Hello Workflow Application

    In this task you will create a very simple workflow that will be the equivalent to this code: C#

    private static void SayHello()

    {

     Console.WriteLine("Hello Workflow 4");

}

    Visual Basic

    Private Shared Sub SayHello()

     Console.WriteLine("Hello Workflow 4")

    End Sub

    1. Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.

    2. Select File / New Project Ctrl+Shift+N and set the project options

    a. Installed Templates: select either Visual C# or Visual Basic then select Workflow

    under the language

    b. Template: Workflow Console Application

    c. Name: HelloWorkflow.

    d. Location: %TrainingKitInstallFolder%\Labs\IntroToWF\Ex1-HelloWorkflow\Begin

    e. Solution: HelloWorkflow

    Figure 1

    Creating a new Workflow Console Application (C#)

Figure 2

    Creating a new Workflow Console Application (Visual Basic)

3. Since your business process is a 1-step process, you can simply add a WriteLine activity to

    implement the process. From the Toolbox, drag a WriteLine activity and drop it on the design surface.

Figure 3

    Adding a WriteLine activity

    Note: If the Toolbox window is not visible, select Toolbox from the View menu.

    4. Set the WriteLine Text property to "Hello Workflow 4".

    Figure 4

    Setting the Text property

     WriteLine Activity

    The WriteLine activity is a simple activity that will display a message on the console. The text

    property is an Expression which could be the result of calling a function or evaluating the

    property of an object. In this case, the expression is a literal string so you must quote the

    string.

Next Step

    Exercise 1 Verification

Exercise 1 Verification

    In order to verify that you have correctly performed all steps of exercise 1, proceed as follows:

    1. Press CTRL+F5 to build and run the workflow without debugging. The application should run in a

    console window and print the message “Hello Workflow 4”.

    Figure 5

    The completed HelloWorkflow application

Report this document

For any questions or suggestions please email
cust-service@docsford.com