DOC

COMHOL09 - Advanced Custom Activities

By Betty Williams,2014-09-09 15:52
9 views 0
COMHOL09 - Advanced Custom Activities

Hands-On Lab

    Lab Manual

    Lab 09 Advanced 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 09: ADVANCED CUSTOM ACTIVITIES ...................................................................................................... 1 Lab Objective .................................................................................................................................................. 1 Exercise 1 Creating a Custom Validation Activity .......................................................................................... 1 Task 1 Creating a Workflow Project .......................................................................................................... 1 Task 2 Creating an Activity Validator ......................................................................................................... 2 Task 3 Building the Project ....................................................................................................................... 3 Exercise 2 Creating a Context Menu for the Activity ...................................................................................... 5 Task 1 Creating a Workflow Project .......................................................................................................... 5 Task 2 Creating an Activity Library Project ................................................................................................ 5 Task 3 Adding a Context Menu to the Activity ........................................................................................... 5 Task 4 Building the Project ....................................................................................................................... 7 Exercise 3 Building Black and White Box Activities........................................................................................ 8 Task 1 Creating a Workflow Project .......................................................................................................... 8 Task 2 Re-using the SendEmail activity .................................................................................................... 8 Task 3 Creating a new activity .................................................................................................................. 9 Task 4 Promoting Properties..................................................................................................................... 9 Task 5 Creating the Black Box activity ...................................................................................................... 9 Task 6 Building the Workflow .................................................................................................................. 10 Lab Summary ................................................................................................................................................ 12

    Page i

Lab 09: Advanced Custom Activities

    In developing workflow solutions, you may identify activities that reoccur in many workflow scenarios. Windows Workflow Foundation allows you to build libraries of reusable activities for use in multiple workflow projects. Custom activities that you create are available in the graphical, design view of workflows, and may provide a custom pop-up context menu to a workflow designer. In addition, an activity may be classified as “black box,” “grey box,” or “white box,” depending upon the degree of visibility and control the activity provides to the workflow solution developer.

Estimated time to complete this lab: 60 minutes

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

    Lab Objective

    The objective of this lab is to demonstrate how to build reusable activities in Windows Workflow Foundation. You will:

     Learn about the fundamental activity classes, including Activity, Designer and Validator, and how

    to package activities into activity libraries.

     Create a custom validation activity and learn how to create a context menu to provide design-time

    configuration and integration.

     Create custom properties for a workflow activity and make the property values available at

    runtime. You will also create black box and white box Composite activities.

    Exercise 1 Creating a Custom Validation Activity

    In this exercise, you will create an Activity Library project from scratch and add a custom validator to the activity. The validator will ensure that the Composite activity consists of at least two activities. You will then test the activity validator in a console workflow project.

    Task 1 Creating a Workflow Project

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

    Beta | Microsoft Visual Studio 2005 Beta 2

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

    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\Lab9 in the Location field.

    7. Enter the name ValidationSample in the Name field.

    Page 1

8. Click OK.

    9. Visual Studio will open a design view of the new workflow project will open, ready for you to drag-

    and-drop activities onto the workflow.

    Task 2 Creating an Activity Validator

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. Keep the default project name, ActivityLibrary1.

    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. In the Solution Explorer, right click on Activity1.cs.

    10. Select View Code from the pop-up menu.

    11. At the bottom of Activity1.cs, within the namespace ActivityLibrary1, add a

    ValidationActivityValidator class to the activity with a Validate method override, as follows:

    public class ValidationActivityValidator : CompositeActivityValidator

    {

     public override ValidationErrorCollection Validate(

     ValidationManager manager, object obj)

     {

     }

    }

12. An activity validator class contains both design-time and run-time validation logic to ensure that an

    activity is appropriately configured. User-defined validation classes inherit from either

    ActivityValidator or CompositeActivityValidator base classes, depending upon whether your

    activity is a basic or composite activity. Validation occurs automatically during the compilation of a

    workflow and at runtime when a dynamic update of the activity is performed. 13. Add the following code to the body of the Validate method to test that the Composite activity

    contains at least two activities (you can use the snippet: Lab09Exercise01ValidateMethod).

     ValidationErrorCollection validationErrors = new

    ValidationErrorCollection(base.Validate(manager, obj));

     Activity1 validationActivity = obj as Activity1;

     if (validationActivity.ExecutableActivities.Count < 2)

     {

    Page 2

     ValidationError CustomActivityValidationError =

     new ValidationError(

     "This activity requires at least two children.", 2);

     validationErrors.Add(CustomActivityValidationError);

     }

     return validationErrors;

    14. Find the definition of the Activity1 class near the top of the file Activity1.cs.

    15. Add a Validator attribute to this class, as follows:

    [Validator(typeof(ValidationActivityValidator))]

    [ToolboxItem(typeof(ActivityToolboxItem))]

    public partial class Activity1: Sequence

    {

     public Activity1()

     {

     InitializeComponent();

     }

    }

    16. The Validator attribute identifies the custom class that implements the validation logic,

    ValidationActivityValidator, as the validator for the Activity1 class.

Task 3 Building the Project

    1. Select the Build | Build Solution menu command to build the project to make sure you entered

    the code correctly.

    2. This step adds the newly redesigned activity to the toolbox for the workflow.

    3. In the Solution Explorer, double click on Workflow1.cs to open the design view of the workflow.

    4. Drag the Validation activity, Activity1, from the Toolbox onto the Visual Studio 2005 designer for

    Windows Workflow Foundation, referred to as the Visual Studio workflow designer in the

    remainder of this document.

    5. Select the Build | Rebuild Solution menu command.

    6. As the workflow is compiled, the Validate method is called for the activity and it generates a

    validation error, detecting too few children for the composite activity. The validation error is

    displayed in the Error List window:

    Page 3

    7. The Visual Studio workflow designer displays an exclamation icon near the activity to indicate an

    error.

    8. Click on the exclamation icon.

    9. Visual Studio displays a pop-up window with the text you entered for your custom validation error:

10. Select the File | Save All menu command.

    11. Select the File | Close Solution menu command.

    Page 4

Validation classes allow you to verify the correct operation of your activities at both compile-time and run-

    time, ensuring the integrity of your workflow design. In this exercise, you created a validation activity and

    saw how the activity can detect workflow design errors at compile-time.

    Exercise 2 Creating a Context Menu for the Activity In this exercise, you will create a design-time context menu for an activity. 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. Enter the name ContextMenuSample in the Name field.

    6. Click OK.

    7. Visual Studio will open a design view of the new workflow project, ready for you to drag-and-drop

    activities onto the workflow.

Task 2 Creating an Activity Library 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. Keep the default project name, ActivityLibrary1.

    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.

Task 3 Adding a Context Menu to the Activity

    Page 5

    1. In the Solution Explorer, right click on Activity1.cs to display the context menu. 2. Select View Code from the menu to display code for Activity1.cs.

    3. In Activity1.cs, add a DesignerAttribute just above the Activity1 class declaration, as follows:

    [DesignerAttribute(typeof(Activity1Designer))]

    [ToolboxItem(typeof(ActivityToolboxItem))]

    public partial class Activity1: Sequence

    {

     public Activity1()

     {

     InitializeComponent();

     }

    }

    4. In the ActivityLibrary1 namespace, add a new class, Activity1Designer, that derives from the

    ActivityDesigner class

    public class Activity1Designer : ActivityDesigner

    {

    }

5. Within the Activity1Designer class add the following method override (snippet:

    Lab09Exercise02VerbsMethod):

    protected override ActivityDesignerVerbCollection Verbs

    {

     get

     {

     ActivityDesignerVerbCollection NewVerbs =

     new ActivityDesignerVerbCollection();

     NewVerbs.AddRange(base.Verbs);

     NewVerbs.Add(new ActivityDesignerVerb(this,

     DesignerVerbGroup.General,

     "Select this for a MessageBox",

     new EventHandler(this.OnMessageBoxEvent)));

     return NewVerbs;

     }

    }

    6. Within the Activity1Designer class add the following method code:

    private void OnMessageBoxEvent(object sender, EventArgs e)

    {

     System.Windows.Forms.MessageBox.Show("Hello");

    }

    7. In the Solution Explorer, expand ActivityLibrary1, and right click References to view the

    context menu.

    8. Select the Add Reference menu command.

    9. Visual Studio displays the Add Reference dialog box.

    10. Scroll down and select the System.Windows.Forms assembly.

    Page 6

11. Click OK.

Task 4 Building the Project

1. Select the Build | Rebuild Solution menu command.

    2. In the Solution Explorer, double click on Workflow1.cs to switch to the design view of the

    workflow.

    3. Drag Activity1 from the Toolbox and drop onto the workflow at “Drop Activities to create

    Sequential Workflow”.

    4. The Visual Studio workflow designer displays an activity icon for Activity1 in the design area. 5. Right click on the activity icon to bring up the context menu. 6. The new menu item is available in the context menu, as follows:

7. Click the custom item, Select this for a MessageBox in the context menu.

    8. The OnMessageBoxEvent method is invoked, calling MessageBox to display a message. 9. Click OK in the message box.

    10. Select the File | Save All menu command.

    11. Select the File | Close Solution menu command.

In this exercise, you have created an activity with an associated context menu that can be used at

    design-time by the workflow solution developer. Using this mechanism, the component developer

    Page 7

Report this document

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