Define and consume activity delegates in the Workflow Designer

Applies to: yesVisual Studio noVisual Studio for Mac noVisual Studio Code

.NET Framework 4.5 includes an out-of-box designer for the InvokeDelegate activity. This designer can be used to assign delegates to the activity that derive from ActivityDelegate, such as ActivityAction or ActivityFunc<TResult>.

Define an activity delegate

  1. Create a new Workflow Console Application project.


    If you don't see the Workflow project templates, first install the Windows Workflow Foundation component of Visual Studio. For detailed instructions, see Install Windows Workflow Foundation.

  2. Right-click on the project in Solution Explorer and select Add > New Item. Select the Workflow category, and then select the Activity item template. Name the new activity MyForEach.xaml and then select OK.

    The activity opens in the workflow designer.

  3. In the Workflow Designer, click the Arguments tab.

  4. Click Create Argument. Name the new argument Items.

  5. In the Argument type column, select Array of [T].

  6. In the type browser, select Object and then select OK.

  7. Click Create Argument again. Name the new argument Body. In the Direction column for the new argument, select Property.

  8. In the Argument Type column, select Browse for types

  9. In the type browser, enter ActivityAction in the Type Name field. Select ActivityAction<T> in the tree view. Select Object in the dropdown that appears to assign the type ActivityAction<Object> to the argument.

  10. Drag a While activity from the Control Flow section of the toolbox to the designer surface.

  11. Select the While activity, and select the Variables tab.

  12. Select Create Variable. Name the new variable Index.

  13. In the Variable type column, select Int32. Leave the Scope as While, and the Default column blank.

  14. Set the Condition property of the While activity to index < Items.Length;.

  15. Drag an InvokeDelegate activity from the Primitives section of the toolbox to the Body of the While activity.

  16. Select Body in the delegate drop-down.

  17. In the Properties grid for the InvokeDelegate activity, click the ... button in the Delegate Arguments property.

  18. In the Value column of the argument named Argument, enter Items[Index]. Click Ok to close the DelegateArguments dialog.

  19. Drag an Assign activity onto the horizontal line underneath the InvokeDelegate activity. The Assign activity is created, and a Sequence activity is created automatically to contain the two activities in the Body section of the MyForEach activity. The sequence is needed since the Body section can only contain a single activity. Automatically creating a new Sequence activity is a new feature of .NET Framework 4.5.

  20. Set the To property of the Assign activity to index. Set the Value property of the Assign activity to index+1.

    The custom MyForEach activity invokes an arbitrary activity once for each value passed into it through the Items collection, with the values in the collection as inputs for the activity.

Use the custom activity in a workflow

  1. Build the project by pressing Ctrl+Shift+B.

  2. In Solution Explorer, open Workflow1.xaml in the designer.

  3. Drag a MyForEach activity from the toolbox to the designer surface. The activity is in a section of the toolbox with the same name as the project.

  4. Set the Items property of the MyForEach activity to new Object[] {1, "abc"}.

  5. Drag a WriteLine activity from the Primitives section of the toolbox to the Delegate:Body section of the MyForEach activity.

  6. Set the Text property of the WriteLine activity to Argument.ToString().

When the workflow is executed, the console shows the following output:

1 abc