martes, 13 de septiembre de 2011

Custom activities in Workflow Foundation 4.0

In this post, I will show how to build custom activities in workflow foundation 4.0 following the code standards from Microsoft. Furthermore, I will introduce how to run a workflow using Workflow Invoker and Workflow Application. All these examples have been done in WPF using a custom WorkflowDesigner control where a Sequence element is initialized in order to add or remove activities from the toolbox.


For this exercise, three non-composed activities have been created: (1) Calculator activity,(2) Loading parameters from XML file and (3) result reporting values. Before, I will expose some common considerations.
  • Considerations
    • Icon for activities not working in toolbox. This is a bug of WF4. There is a workaround to fix it but needs to include a library and so on and, anyway, in the deployment, this works.
Workaround for icons of the toolbox for workflow designer

    • All activities have a designer activity in wpf. This designer class is specified using the designer attribute in the class of the activity. To create a designer, clicks on Add a new item, then in workflow and selects Activity Designer. In each designer, there is a ModelItem property with a link to the activity using the GetCurrentValue method and using the Root to the main context where to take all variables and so on.
  1. Calculator activity.
This simple activity can be used to write expressions with complex maths operators and using variables within workflow context. This activity shows a calculator control in wpf from extended library set of the wpf toolkit but with some changes:

  • Read the current variables within the workflow context.
  • Disable the operators because this mechanism is to write expressions.
This control has been used because some many things already are included like accessibility, keyboard support, enumerators, xaml templates...



This activity is a CodeActivity which contains a InArgument of type decimal where to receive the expression. This activity inherits to CodeActivity with decimal type. The most relevant here, it is that a new variable is created when a calculator activity is added in the workflow with the result of the expression. Therefore, you will be able to split expresions in several activities. In the execute method, the result of the expression will already be calculated, then only will be necessary to update the result value.

The ResultChangedEvent is raised when the value is calculated in order to update the variable in the proper context. The designer will listen this event and will do it.
In next post, it will be explained how to read and update variables within the context, and also set its value, will be exposed.

    2.   Loading parameters from XML file.

This activity read a XML file with a special element where the parameters will be specified. In the designer, the user will be able to modified the names in order to avoid possible collisions. The activity inherits to CodeActivity class and in the execute, will read the value of the parameters from the xml file and will update the value into previously created variables.


You can check in runtime if a variable name is available or enable/disable some of them.


   3.   Result reporting values.

This activity reports the variable values specified in the designer into the output of the workflow control created.

And that is all! :)

No hay comentarios:

Publicar un comentario