DOC

COMHOL02 - Creating Custom Activities

By Carlos Graham,2014-09-09 15:47
11 views 0
COMHOL02 - Creating Custom Activities

Hands-On Lab

    Lab Manual

    Lab 02 Creating Custom Activities

    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 02: CREATING CUSTOM ACTIVITIES ............................................................................................................. 1 Lab Objective ......................................................................................................................................................... 1 Exercise 1 Creating a Composite Activity Using the Activity Designer ............................................................... 1 Task 1 Create a new Workflow Project ........................................................................................................... 1 Task 2 Creating an Activity Project ................................................................................................................. 3 Task 3 Building the Workflow Solution ........................................................................................................... 5 Exercise 2 Adding Custom Properties to an Activity ........................................................................................... 6 Task 1 Creating a Workflow Project ............................................................................................................... 7 Task 2 Creating an Activity Project ................................................................................................................. 7 Task 3 Building the Workflow Solution ......................................................................................................... 11 Exercise 3 Updating the Basic Custom Activity to Send E-Mail........................................................................ 12 Task 1 Adding an Executor Component ....................................................................................................... 12 Task 2 Building the Project ........................................................................................................................... 13 Task 3 Updating the Activity to Send Email .................................................................................................. 13 Exercise 4 Adding Verification to Check E-Mail Parameters ............................................................................ 15 Task 1 Adding an Email Verification Code Activity ....................................................................................... 15 Task 2 Testing The Validator ........................................................................................................................ 17 Lab Summary ....................................................................................................................................................... 18

    Page i

Lab 02: Creating Custom Activities

    Windows Workflow Foundation is a workflow-based programming model that allows you to design solutions for your business processes. Visual Studio 2005 Extensions for Windows Workflow Foundation provides a set of programming tool for this purpose. A workflow is a network of activities and conditions that describe a complete business process. The workflow programming model encompasses automated information processing as well as tasks performed by people. With isual Studio 2005 Extensions for Windows Workflow Foundation, you can design workflows visually in Visual Studio, by dragging activities from a toolbox onto a visual design area for the workflow. You can also create activities used in workflows by writing managed code, building upon integrated Workflow Foundation templates.

Estimated time to complete this lab: 60 minutes

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

Lab Objective

    The objective of this lab is to demonstrate how to create custom activities in Windows Workflow Foundation. You will also learn how to expose properties on an activity in a workflow and access the property values at runtime. The lab consists of four exercises. In the first exercise, you will create a composite activity using Visual Studio‟s built-in Activity Designer. Next, you will create another workflow

    project and activity, and add custom properties to the activity. You will then extend the second exercise to add an executor component to the activity that will use the custom properties as input when sending e-mail. In the last exercise, you will add a validation component to the workflow to verify the incoming e-mail parameters.

    Exercise 1 Creating a Composite Activity Using the Activity Designer In this exercise, you will create an activity that consists of two separate code activities. You will use the resulting composite activity in a workflow project, running the project to see the output produced by the code activities.

Task 1 Create a new Workflow Project

    Workflow projects require a host to execute the workflow. For this exercise, you will create a console application to host and interact with your workflow process.

    1. Navigate to Start | All Programs | Microsoft Visual Studio 2005 Beta 2 | Microsoft Visual

    Studio 2005 Beta 2.

    2. Select the File | New | Project menu command.

    Page 1

3. Visual Studio displays the New Project dialog box.

    4. Select the Visual C# | Workflow project type.

    5. Select the Sequential Workflow Console Application template.

    6. Enter the path C:\Windows Workflow Foundation\Labs\Lab02 in the Location field.

    7. Enter the name CompositeActivitySample in the Name field.

    8. Click OK.

    9. Visual Studio creates a Workflow console application and opens the Visual Studio workflow

    designer displaying a graphical representation of the workflow.

    10. There are four tabs in the lower left of the graphical representation window, as follows:

    11. Each tab provides a different view of the workflow. The leftmost tab shows the activity-based

    view of the workflow, the second tab shows the exception handlers for the workflow, the third

    tab shows the event handlers for the workflow, and the rightmost tab shows the compensation

    handlers for the workflow.

    Page 2

    12. Click each tab to see the different views of the workflow.

    13. Click the leftmost tab to return to the Designer view.

    14. In the Solution Explorer, right click on Program.cs select View Code.

    15. Visual Studio displays Program.cs in the main window.

    16. Program.cs contains the Main() method for your workflow application. It contains boilerplate

    code to start the workflow runtime, load and execute the workflow, and then wait for the

    workflow to complete. The generated code includes an event handler, OnWorkflowCompleted,

    that sets an event to signal the main method that the workflow has completed. The main method

    then continues and shuts down the workflow runtime.

    17. In the Solution Explorer, right-click on the Workflow1.cs file and rename it to

    HelloWorldWorkflow.cs.

    18. If the Visual Studio displays the following dialog, click “Yes”

    19. In the Solution Explorer, double click HelloWorldWorkflow.designer.cs to see the support

    code for the workflow. Visual Studio displays HelloWorldWorkflow. designer.cs. This code

    maintains the design description of the workflow.

Task 2 Creating an Activity Project

    Now that you have created a Workflow project, the next step is to add an Activity project to the Workflow project. Once you have created the Activity project, you will add two Code activities to the Activity project. The Code activities will execute one after another to demonstrate sequential execution of business process elements. At a very high level, it can be said that there are two types of activities:

    ; Primitive

    ; Composite

    In other words, new activities can be defined either by creating new primitive activities where they are implemented from scratch or, they can be composed of other existing activities as composite activities. The activity we will be creating in this exercise will be composing of two code activities, so it falls into the category of “composite” activities.

    1. Select the File | New | Project menu command.

    2. Visual Studio displays the New Project dialog box.

    3. Select the Visual C# | Workflow project type.

    4. Select the Workflow Activity Library template.

    5. Rename the default project name, ActivityLibrary1 to HelloWorldActivityLibrary

    6. Select Add to Solution from the Solution dropdown list.

    Page 3

7. Click OK.

    8. Visual Studio creates the activity library project, including a single activity, Activity1, and

    displays a design view of Activity1.cs.

    9. Please rename Activity1.cs to HelloWorldActivity.cs

    10. Click on the HelloWorldActivity icon or surrounding work area to view the properties for

    HelloWorldActivity in the Properties window.

    11. Select the View | Toolbox menu command to open the Toolbox window. 12. Click on the push pin icon in the upper right of the Toolbox window to disable auto-hiding the

    window.

    13. In the Toolbox window, expand Windows Workflow and drag a Code activity and drop it onto

    the Drop Activities Here” work area of HelloWorldActivity „s design view.

    14. The Activity Designer displays a code icon in the work area. 15. Change the (ID) property of the code activity to writeHello.

    16. Double click the writeHello activity to view the writeHello_ExecuteCode method.

    17. Type the following code in the writeHello_ExecuteCode method body.

    Console.Write("Hello, ");

    18. Double click HelloWorldActivity.cs in the Solution Explorer to return to the Visual Studio

    workflow designer.

    Page 4

    19. Drag a second Code activity onto the design area below the first code activity and change the

    (ID) property to writeWorld.

    20. Double click the Code icon you just placed onto the design area to open the code view of the

    writeWorld _ExecuteCode method.

    21. Type the following code in the writeWorld _ExecuteCode method body.

    Console.WriteLine("World");

22. The code for the HelloWorldActivity class should now look like this:

    Task 3 Building the Workflow Solution

    You will now add the newly created activity to the workflow and run the solution.

1. Select the Build | Build Solution menu command.

    2. In the Solution Explorer, double click on HelloWorldWorkflow.cs to switch to the Design

    view of the workflow.

    3. Notice that there is a new entry in the Toolbox list, HelloWorldActivityLibrary Components,

    under which the activity created in the last Task, HelloWorldActivity, appears.

    Page 5

    4. Drag HelloWorldActivity onto the Visual Studio workflow designer and drop it onto the area

    labeled “Drop Activities to create Sequential Workflow”. Change the (ID) property of the activity

    to helloWorld.

    5. The Visual Studio workflow designer updates the display to include HelloWorldActivity,

    including the two code activities, as follows:

    6. Select the Build | Build Solution menu command.

    7. Select the Debug | Start Without Debugging menu command to run the program and see the

    output.

    8. You will see “Hello, World” displayed in the output console window and a prompt to press any

    key to continue.

    9. Press the Enter key to end the program.

    10. Select File | Save All.

    11. Select File | Close Solution.

Exercise 2 Adding Custom Properties to an Activity

    In this exercise, you will create another workflow project that contains an activity with custom properties. You will use the custom properties to specify fields for an e-mail message. You will create an executor component to a basic custom activity and add it to the workflow. The executor component will use .NET APIs and the data contained in the fields to send an e-mail message.

    Page 6

    Task 1 Creating a Workflow Project

1. Select the File | New | Project menu command.

    2. Visual Studio displays the New Project dialog box.

    3. Select the Visual C# | Workflow project type.

    4. Select the Sequential Workflow Console Application template. 5. Make sure the Location field is set to C:\Windows Workflow Foundation\Labs\Lab02. 6. Enter the name CustomPropertySample in the Name field.

    7. Click OK.

    8. Visual Studio creates a workflow console application and opens the Visual Studio workflow

    designer, displaying a graphical representation of the workflow.

    9. Rename the Workflow1.cs workflow to SendMailWorkflow.cs.

    Task 2 Creating an Activity Project

1. Select the File | New | Project menu command.

    2. Visual Studio displays the New Project dialog box.

    3. Select the Visual C# | Workflow project type.

    4. Select the Workflow Activity Library template.

    5. Change the default project name to SendMailActivityLibrary.

    6. Select Add to Solution from the Solution dropdown list. 7. Click OK.

    8. Visual Studio creates the activity library project, including a single activity, Activity1, and

    displays a graphical design view of Activity1.

    9. Rename Activity1.cs to SendMailActivity.cs.

    10. Click on SendMailActivity title just above the “Drop Activities Here” work area to view the

    properties for SendMailActivity in the Properties window.

    11. In the Properties window, select the Base Class property.

    12. Click the “…” button associated with the Base Class property. 13. The Browse and Select a .NET Type dialog box appears allow you to select a base class for

    this activity.

    Page 7

Report this document

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