DOC

COMHOL04 - Creating State Machine Workflows

By Victor Woods,2014-09-09 15:49
8 views 0
COMHOL04 - Creating State Machine Workflows

Hands-On Lab

    Lab Manual

    Lab 04 Creating State Machine Workflows

    Please do not remove this manual from the lab

     ;

    Information in this document, including URL and other Internet Web site references, is subject to change without notice. This document supports a preliminary release of software that may be changed substantially prior to final commercial release, and is the proprietary information of Microsoft Corporation.

    This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, AS TO THE INFORMATION IN THIS DOCUMENT.

    The entire risk of the use or the results from the use of this document remains with the user. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

    Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

? 2005 Microsoft Corporation. All rights reserved.

    Microsoft, MS-DOS, Windows, Windows NT, Windows Server, Visual C# and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

    The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

     ;

Contents

    LAB 04: CREATING STATE MACHINE WORKFLOWS...................................................................................... 1 Lab Objective .................................................................................................................................................. 1 Exercise 1 Create the Order State Machine Workflow ................................................................................... 1 Task 1 Create a new State Machine Workflow Project .............................................................................. 2 Options on how to proceed in this exercise .................................................................................................. 4 Task 2 Create the Order data exchange service interface ......................................................................... 5 Task 3 Define the Implementation of the IOrderService interface .............................................................. 9 Task 4 Define the States for the Order State Machine Workflow ............................................................. 12 Task 5 Define Workflow variables to bind event payload to workflow. ...................................................... 17 Task 6 Define the WaitingForOrderState................................................................................................. 17 Task 7 Define the OrderOpenState ......................................................................................................... 23 Task 8 Define the OrderProcessedState ................................................................................................. 34 Task 9 Add the OrderApplication Project................................................................................................. 49 Task 10 Test the State Machine Workflow .............................................................................................. 51 Exercise 2 Use the StateMachineTracking Service...................................................................................... 53 Task 1 Add a reference to the StateMachineTracking Service ................................................................. 53 Task 2 Modify the code in the OrderApplication ...................................................................................... 54 Task 3 Test the OrderApplication and State Machine Tracking................................................................ 59 View Completed Labs .................................................................................................................................... 60 Lab Summary ................................................................................................................................................ 61

    Page i

Lab 04: Creating State Machine Workflows

    Estimated time to complete this lab: 60 minutes

    The project files for this lab are in the C:\Windows Workflow Foundation\Labs\Lab04 folder.

Lab Objective

    The objective of this lab is to introduce another workflow authoring style known as State Machine Workflows. State machine workflows offer a flexible workflow creation style where the process is modeled as a state machine. Unlike a sequential workflow where the activities execute in a sequence in a state machine workflow the activities execute based on external events. The state machine responds to an external event, does the required work and then moves to the next state.

    After completing this lab, you will be able to:

     Author state machine workflows using the Visual Studio 2005 designer for Windows Workflow

    Foundation, referred to as the Visual Studio workflow designer in the remainder of this

    document

     Utilize the State and SetState activities

     Utilize the sample State Machine Tracking Service to inspect and track the state of a workflow

    from the host application

    In case you are not able to finish the labs you can view the completed labs that have been done. Go to the View Completed Labs section to find out how to view them.

    Exercise 1 Create the Order State Machine Workflow

    In this exercise, you will create a state machine workflow for managing an order. You can imagine an e-commerce application where an order might go through several states before it is completed. For our purposes, we’ll define the following four states:

     WaitingForOrderState

     OrderOpenState

     OrderProcessedState

     OrderCompletedState

    In our order system there are also rules specifying what events can occur on an order depending on its current state. For example, an Order that is open can be updated, processed, canceled, or shipped. However, an order that is processed, can only be updated or shipped, but cannot be canceled. As an event occurs, our state machine workflow can optionally transition the state for an order. For example, when an order is open and the OrderShipped event occurs, our state machine workflow will transition the completed state for the order. The states and transitions for our simple Order state machine

    Page 1

workflow are illustrated in the following diagram:

Figure 1 : Order Workflow

    We will start creating the Order state machine workflow by modeling the possible states for an order entity by using the State activity. Then we will specify the events that can occur with each state by using EventDriven activities and custom activities for the order events. In most cases when an order event occurs we will transition the state of the order using the SetState activity. Finally, after authoring our workflow we will test it using a pre-built Windows Forms host.

Task 1 Create a new State Machine Workflow Project

    1. Open Visual Studio 2005 by going to the Start Menu | All Programs | Microsoft Visual Studio

    2005 Beta 2 | Microsoft Visual Studio 2005 Beta 2

    2. In Visual Studio 2005, select the File | New | Project menu;command.

    3. Visual Studio will display the New Project dialog window.

    Page 2

    4. In the New Project dialog window, expand Visual C# | Workflow in the Project Types tree on

    the left side.

    5. Select the template named State Machine Workflow library and enter the following values for

    the name and location:

    Name: OrderWorkflows

    Location: C:\Windows Workflow Foundation\Labs\Lab04

    Figure 2 : Select State Machine Workflow Library

    6. You should now have a new solution and workflow project. 7. Note also that under the project OrderWorkflows there is a file called Workflow1.cs added

    Page 3

    Figure 3 Workflow1.cs File

8. Also you can see that the state machine designer is displayed.

     Figure 4 Free Form State Machine Designer

    Options on how to proceed in this exercise

    The next 2 tasks (Tasks 2 and Task 3) deal with the creation of a local service, the mechanism

    used to facilitate communication with the workflow. In order to send data to the workflow, the host can raise events which are subscribed to by activities inside the workflow. In order for the workflow to send data to the host, the workflow can call methods. These methods and events are defined in an interface called the data exchange service. Task 2 creates the data exchange service interface and task 3 creates an implementation of the interface.

    Page 4

    You have a choice at this point. If you are already familiar with the creation of the data exchange service then you can simply skip Tasks 2 and Task 3. If you have skipped Task 2 and 3 we will

    simply add a reference to the OrderLocalService.dll in the OrderWorkflows project.

    Task 2 Create the Order data exchange service interface

    1. Before we can begin building the workflow we need to create the local service in order to

    communicate the right set of events to the workflow.

    2. In order to do that, right click on the solution and add a new project of type Windows Class

    Library.

    3. Name the project as OrderLocalServices

Figure 5 Create the OrderLocalServices project as a Windows Class Library

    4. By default there is a file called ‘class1.cs’ added to the project. Right click on the file and delete

    it.

    5. Now we need to add references to the project because we will be referring types from the

    system.workflow.* namespaces

    6. Add the following references to the project by right clicking on the references folder

    Page 5

     Figure 6 Add references to the OrderLocalService project

    a. The following references need to be added

    i. System.Workflow.ComponentModel

    ii. System.Workflow.Runtime

    Figure 7 Add references to the System.Workflow.* assemblies

    Page 6

    7. Now we need to create the local service interface where we will define all the events that we

    want to raise on the workflow. The events we want to raise will be the events that happen

    externally to the workflow. The workflow will respond to these events and change state

    accordingly. The events we want to create are

    a. Order Created

    b. Order Updated

    c. Order Processed

    d. Order Canceled

    e. Order Shipped

8. In order to create the interface add a new class to the project and call it IOrderService.cs

Figure 8 add a new class IOrderService to the OrderLocalServices project

    9. Delete the class definition for IOrderService. We will be adding the interface IOrderService to

    this file. The text to be deleted is inside the namespace and is as follows

     class IOrderService

     {

    }

    Code Snippet 1 Code to be deleted from IOrderService.cs

    10. Now we need to define the interface IOrderService. Also note that in the interface we will be

    adding the 5 events mentioned in step 7.

    Page 7

Report this document

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