DOC

COMHOL03 - Hosting Workflows

By Anne Owens,2014-09-09 15:48
7 views 0
COMHOL03 - Hosting Workflows

Hands-On Lab

    Lab Manual

    Lab 03 Hosting the Windows Workflow Foundation Runtime and Running 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, SQL 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 03: HOSTING THE WINDOWS WORKFLOW FOUNDATION RUNTIME AND RUNNING WORKFLOWS .. 1 Lab Objective .................................................................................................................................................. 1 Exercise 1 Build a Windows Console Host .................................................................................................... 1 Task 1 Create a new Console Application Project ..................................................................................... 1 Exercise 2 Runtime Events ........................................................................................................................... 8 Task 1 Listen for Runtime Events ............................................................................................................. 8 Exercise 3 Using Runtime Services............................................................................................................. 11 Task 1 Configuring Runtime Services ..................................................................................................... 11 Exercise 4 Starting and Running Workflows ................................................................................................ 15 Task 1 Starting a Workflow ..................................................................................................................... 15 Task 2 Starting Workflows with Parameters ............................................................................................ 17 Exercise 5 Using the SqlTrackingService .................................................................................................... 23 Task 1 Adding the SQLTrackingService to the host environment............................................................. 24

    Page i

    Lab 03: Hosting the Windows Workflow Foundation Runtime and Running Workflows

    Estimated time to complete this lab: 60 minutes

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

Lab Objective

    The objective of this lab is to introduce you to hosting the Windows Workflow Foundation runtime. After completing this lab, you will be able to:

     Host the workflow runtime in a simple Windows console application and run a workflow using

    the workflow runtime.

     Run the console application to verify that workflow was loaded, ran successfully and that the

    runtime events are raised.

     Use the SQLTracking service to log workflow and runtime related events.

     Use an app.config file to tailor the runtime environment to meet your needs.

Exercise 1 Build a Windows Console Host

    In this exercise you will create a simple console application to instantiate the workflow runtime and start a workflow.

Task 1 Create a new Console Application 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.

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

    the left side.

    5. Select Sequential Workflow Console Application” and accept the default value for the name

    but change the location to c:\Windows Workflow Foundation\Labs\Lab03\. Leave the "Create

    directory for solution" box checked. This will create a subdirectory to c:\Windows Workflow

    Foundation\Labs\Lab03 and place the project and solution files there. The directory will be

    named WorkflowConsoleApplication1.

    Page 1

    Name: WorkflowConsoleApplication1

    Location: c:\Windows Workflow Foundation\Labs\Lab03\

    6. You should now have a new solution with a Workflow console application project that includes Program.cs, which contains the definition for Main, and a sequential workflow contained in Workflow1.cs. You should see something like this:

    Page 2

    7. First, let's take a look at the code generated in Program.cs. Open Program.cs in the editor so

    that we can inspect the code. Note that CLR data types will appear in bold. It should look like

    this:

    #region Using directives

     using System;

     using System.Collections.Generic;

     using System.Text;

     using System.Threading;

     using System.Workflow.Runtime;

     using System.Workflow.Runtime.Hosting;

     #endregion

     namespace WorkflowConsoleApplication1

     {

    Page 3

     class Program

     {

     static AutoResetEvent waitHandle = new AutoResetEvent(false);

     static void Main(string[] args)

     {

     WorkflowRuntime workflowRuntime = new WorkflowRuntime();

     workflowRuntime.StartRuntime();

     workflowRuntime.WorkflowCompleted += OnWorkflowCompleted;

     Type type = typeof(Workflow1);

     workflowRuntime.StartWorkflow(type);

     waitHandle.WaitOne();

     workflowRuntime.StopRuntime();

     }

     static void OnWorkflowCompleted(object sender,

     WorkflowCompletedEventArgs e)

     {

     waitHandle.Set();

     }

     }

    }

    8. If we look at the references for this project we will see that it comes with references to the

    following workflow components, System.Workflow.Activities, System.Workflow.ComponentModel and System.Workflow.Runtime

    Page 4

:

    9. Let's go back to Workflow1 in the Visual Studio workflow designer and add a code activity to it.

    After you have dropped a code activity on the workflow design surface you should see

    something like this:

    Page 5

    10. The red ball with the exclamation mark is an indication that we need to set at least one of the

    properties for code1. In this case we need to set the ExecuteCode property. You can see this

    by clicking on the red ball with the exclamation and then clicking on the down arrow. You should

    see something like this:

    11. If you look at the property window you will see that there is a corresponding little red ball with an

    exclamation mark indicating that the ExecuteCode property needs to be set. In fact, if you tried

    to compile the application right now you would get an error indicating that “Activity 'code1'

    validation failed: Property 'ExecuteCode' is not set.

    12. The simplest way to do this is to double click on the code1 activity drawing when it has focus in

    the workflow design surface. Double clicking will then generate code1_ExecuteCode. When

    code1_ExecuteCode was generated by Visual Studio it also connected the ExecuteCode

    handler for code1 to code1_ExecuteCode as you can see from this screen shot.

    Page 6

    13. When the workflow is executed the code handler for code1 will be called so let's just add a simple Console.WriteLine statement to code1_ExecuteCode so that we know the function was called. Add the code highlighted in gray:

private void code1_ExecuteCode(object sender, EventArgs e)

    {

     Console.WriteLine("Here in code1_ExecuteCode.");

    }

    14. Now that the code activity in the workflow can indicate that it has been run let’s go back to the console application and add some code to Program.cs. The code already generated in Main is all we need to create an instance of the workflow and run it. However, it will complete and exit before you have a chance to see what it displayed. To introduce a pause in the application so that you can see what happened add the two lines of code highlighted in gray to the OnWorkflowCompleted function.

    static void OnWorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

    {

     Console.WriteLine("In OnWorkflowCompleted, press ENTER to exit.");

     Console.Read();

     waitHandle.Set();

    }

    15. Now select the Build | Build Solution menu command to compile and build your project. Verify that there are no compiler errors or warnings. If all goes well then press F5 to run the console

    application. You should see something like the following:

    Page 7

Report this document

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