DOC

COMHOL11 - Authoring Workflows Through Code and Dynamic Update

By Stacy Burns,2014-09-09 15:54
7 views 0
COMHOL11 - Authoring Workflows Through Code and Dynamic Update

Hands-On Lab

    Lab Manual

    Lab 11 Authoring Workflows through Code and Dynamic Update

    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 11: AUTHORING WORKFLOWS THROUGH CODE AND DYNAMIC UPDATE .......................................... 1 Lab Objective .................................................................................................................................................. 1 Exercise 1 Sequential Workflow with Parameters .......................................................................................... 1 Task 1 Create a new Workflow Project...................................................................................................... 1 Task 2 Create a new Workflow in Code .................................................................................................... 1 Task 3 Add the Program Code .................................................................................................................. 6 Task 4 Call the Code with Parameters ...................................................................................................... 7 Exercise 2 Dynamic Update in Workflow ....................................................................................................... 9 Task 1 Create a new Workflow Project...................................................................................................... 9 Task 2 Create the First Workflow ............................................................................................................ 10 Task 3 Add a Parameter to the Workflow ................................................................................................ 12 Task 4 Create the Second Workflow ....................................................................................................... 12 Task 5 Add the Program Code ................................................................................................................ 12 Task 6 Run the Workflow ........................................................................................................................ 14 Exercise 3 Dynamic Update from Host ........................................................................................................ 15 Task 1 Creating the Workflow ................................................................................................................. 15 Task 2 Run the Workflow ........................................................................................................................ 15 Task 2 Modifying the First Workflow........................................................................................................ 16 Task 4 Run the Workflow ........................................................................................................................ 16 Exercise 4 If Else Dynamic Update ............................................................................................................. 18 Task 1 Creating the Workflow ................................................................................................................. 18 Task 2 Run the Workflow ........................................................................................................................ 18 Task 2 Modifying the Workflow ............................................................................................................... 20 Task 4 Run the Code .............................................................................................................................. 20 Lab Summary ................................................................................................................................................ 21

    Page i

    Lab 11: Authoring Workflows through Code and Dynamic Update Estimated time to complete this lab: 60 minutes. The last two exercises are optional.

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

    Lab Objective

    The objective of this lab is to demonstrate how to author workflow through code, and apply dynamic changes to a workflow at run-time. After completing this lab, you will be able to:

     Author workflows purely from code using the Windows Workflow Foundation Object Model

     Apply dynamic changes to a workflow at run-time

    Exercise 1 Sequential Workflow with Parameters

    In this exercise, you will create a workflow programmatically. You will then compile and run the workflow. This particular workflow has two branches that can be executed depending on the value of a parameter supplied by the user. If the amount specified in the parameter for the order is less than or equal 500 then the order will be approved and the program will complete. If the amount specified is greater than 500 then the order will be rejected and the program will complete. The workflow defines an In parameter “amount” to

    specify the order amount and an Out parameter “status” to report the result of the approval.

    Task 1 Create a new Workflow Project

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

    2005 Beta 2 | Microsoft Visual Studio 2005 Beta 2.

    2. In Visual Studio 2005, select File | New | Project.

    3. In the New Project dialog box, expand Visual C# | Workflow in the Project types tree on the left

    4. Select the template Sequential Workflow Console Application from the Visual Studio installed

    templates and type the following for the name and location and click OK.

    Name: SequentialWorkflowWithParameters

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

Task 2 Create a new Workflow in Code

    We will create the workflow in code from scratch for this exercise. We will not be using the provided template.

    5. Select the Workflow1.cs file in the solution file and press the Delete key.

    6. Right click on the project and choose Add | New Item. Select Code File, name it Workflow1.cs

    and click Add.

    Page 1

    7. Add the following to Workflow1.cs to create a basic sequential data workflow.

    using System;

    using System.Workflow.ComponentModel; using System.Workflow.Activities;

    namespace SequentialWorkflowWithParameters {

     public partial class BasicSequentialWorkflow : SequentialWorkflow

     {

     private IfElse ifElse1;

     private IfElseBranch ifElseBranch1;

     private IfElseBranch ifElseBranch2;

     private Code code1;

     private Code code2;

     public BasicSequentialWorkflow()

     {

     InitializeComponent();

     }

     private void InitializeComponent()

     {

     }

     }

    }

    Page 2

    8. In Solution Explorer right-click Workflow1.cs and select View Designer to view the workflow so far. Not a lot to see, but we have code which is viewable in the designer without error.

    9. Right-click on the file Workflow1.cs in the solution explorer and choose View Code. You'll start by creating the decision-making branch of the workflow. You can now add an ifElse activity

    programmatically. Add the following code within the InitializeComponent() method:

     amount = new ParameterDeclaration();

     status = new ParameterDeclaration();

     ActivityBind activitybind1 = new ActivityBind();

     ActivityBind activitybind2 = new ActivityBind();

     CodeCondition codecondition1 = new CodeCondition();

     this.ifElse1 = new IfElse();

     this.ifElseBranch1 = new IfElseBranch();

     this.ifElseBranch2 = new IfElseBranch();

     //

     // ifElse1

     //

     this.ifElse1.Activities.Add(this.ifElseBranch1);

     this.ifElse1.Activities.Add(this.ifElseBranch2);

     this.ifElse1.ID = "ifElse1";

     //

     // ifElseBranch1

     //

     // code condition to check request amount

     codecondition1.Condition += new ConditionalExpression(CheckAmount);

    Page 3

     this.ifElseBranch1.Condition = codecondition1;

     this.ifElseBranch1.ID = "ifElseBranch1";

     //

     // ifElseBranch2

     //

     this.ifElseBranch2.Condition = null;

     this.ifElseBranch2.ID = "ifElseBranch2";

     //

     // BasicSequentialWorkflow

     //

     this.Activities.Add(this.ifElse1);

     this.DynamicUpdateCondition = null;

     this.ID = "Workflow1";

    10. In Solution Explorer right-click Workflow1.cs and select View Designer to view the workflow so far.

    11. You can now add two Code activities to the ifElse branches programmatically. Add the following

    code at the bottom of the InitializeComponent() method to add Code activity to each branch.

     this.code1 = new Code();

    Page 4

     this.code2 = new Code();

     this.ifElseBranch1.Activities.Add(code1);

     this.ifElseBranch2.Activities.Add(code2);

    12. Now add the code to the end of the InitializeComponent() method to configure the two code activities

    to point to event handlers that we will add in the next steps // Set up the code activities and handlers

    // code1

    this.code1.ID = "code1";

    this.code1.ExecuteCode += new EventHandler(Approved);

// code2

    this.code2.ID = "code2";

    this.code2.ExecuteCode += new EventHandler(Rejected);

    13. Add the parameter declarations inside the BasicSequentialWorkflow class declaration

     ParameterDeclaration amount = null;

     ParameterDeclaration status = null;

    14. Now you can add the business logic of the workflow. Contained here is the CodeCondition that the

    IfElseBranch will evaluate, as well as the event handlers for the Code activities we configured previously. Add the following code at the end of the BasicSequentialWorkflow class declaration:

    // Code condition to evaluate whether to take the first branch, ifElseBranch1 // Since it always returns true, the first branch is always taken. private bool CheckAmount(object sender, EventArgs e)

    {

     Int32 orderAmount = Convert.ToInt32(amount.Value);

     Console.WriteLine("Order Amount = " + orderAmount);

     if (orderAmount <= 500)

     return true;

     else

     return false;

    }

private void Rejected(object sender, EventArgs e)

    {

     status.Value = "Rejected";

    }

private void Approved(object sender, EventArgs e)

    {

     status.Value = "Approved";

    }

    15. Click the Workflow1.cs tab and add the parameters definitions to the end of the InitializeComponent() method. Add the following code within the existing code:

    Page 5

     //

     // Parameter declarations

     //

     amount.Direction = ParameterDirection.In;

     amount.Name = "amount";

     amount.Type = typeof(System.Int32);

     amount.Value = null;

     status.Direction = ParameterDirection.Out;

     status.Name = "status";

     status.Type = typeof(System.String);

     status.Value = null;

     this.Parameters.Add(amount);

     this.Parameters.Add(status);

    Task 3 Add the Program Code

    1. In Solution Explorer double-click on Program.cs.

    2. Now we need to create a simple console host that will start our workflow and pass a parameter into

    it and receive one back. Replace the code in Program.cs with the following.

    using System;

    using System.Collections.Generic;

    using System.Threading;

    using System.Workflow.Runtime;

namespace SequentialWorkflowWithParameters

    {

     class Program

     {

     //A workflow is always run asynchronously; the main thread waits

     //on this event so the program doesn't exit before the workflow

     //completes

     static AutoResetEvent waitHandle = new AutoResetEvent(false);

     static void Main(string[] args)

     {

     if (args.Length < 1)

     {

     Console.WriteLine("Usage: program [amount]");

     return;

     }

     // Create and start the WorkflowRuntime

     WorkflowRuntime workflowRuntime = new WorkflowRuntime();

     Console.WriteLine("Runtime Started.");

     // Load the workflow type

     Type type = typeof(BasicSequentialWorkflow);

    Page 6

     // Listen for the workflow completed event

     workflowRuntime.WorkflowCompleted += OnWorkflowCompletion;

     // Set up the parameters

     Dictionary parameters = new Dictionary

     object>();

     parameters.Add("amount", Convert.ToInt32(args[0]));

     parameters.Add("status", "InitialStatus");

     // Create an instance of the workflow

     workflowRuntime.StartWorkflow(type, parameters);

     Console.WriteLine("Workflow Started.");

     // Wait for the event to be signaled

     waitHandle.WaitOne();

     Console.WriteLine("Program Complete.");

     }

     // This method will be called when a workflow instance is idle;

     // since we have started only a single instance we are ignoring

     // the event args and signaling the waitHandle so the main thread

     // can continue

     static void OnWorkflowCompletion(object sender,

     WorkflowCompletedEventArgs workflowCompletedEventArgs)

     {

     string orderStatus =

     workflowCompletedEventArgs.OutputParameters["status"].ToString();

     Console.WriteLine("Order was " + orderStatus);

     waitHandle.Set();

     }

     }

    }

    Task 4 Call the Code with Parameters

    1. Build the solution.

    2. Click the PC’s Start menu and then click Run. Type cmd and click OK.

    3. At the command prompt change to the following directory: c:\Windows Workflow

    Foundation\labs\lab11\SequentialWorkflowWithParameters\SequentialWorkflowWithParamete

    rs\bin\debug

    4. At the command prompt type the following command: SequentialWorkflowWithParameters 400

    Page 7

Report this document

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