In WF 3.5 it was possible to enable and disable activity execution by simply changing the Enabled property in the designer property grid. This would turn on or off both validation and execution of the activity and was really useful in scenarios where you would want to quickly and easily switch out sections of your workflow without having to alter the structure. Unfortunately the ability to do this in WF4 is missing. As always there are workarounds. For example you could use the CommentOut custom activity in Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 but this only allows you to disable and enable child activities by dragging them into and out of the custom activity's drop zone. You could also use the standard If activity and place child activities into the appropriate branch then simply set the condition to either "true" or "false". But this isn't at all intuitive and obfuscates the workflow very quickly. Also neither of these options allow you to disable validation which may be useful if you temporarily want to ignore errors while the activity is disabled.

I therefore decided to create the SelectiveExecution custom activity which is a composite activity that allows you to easily enable and disable its child activities at design time. You can download the source code here.

Disabling an activity prevents execution and validation of all child activities. Enabling or disabling is simply a case of changing the Execute property in the property grid or clicking the Stop/Start button in the designer. It leaves the workflow design clean and intuitive.

Drop the SelectiveExecution activity onto the design surface:

image_2_5CFCBF4C

Drop an activity into the drop zone and click the button to enable or disable its validation and execution. That's it.

image_4_5CFCBF4C

You can download the source here complete with a demo project that shows the activity in action:

image_6_5CFCBF4C

If you study the workflow above then it may come as no surprise that when you run it the only output you will see is "WriteLine2". The output of "WriteLine4" is not displayed since its parent is disabled. You could argue that the child's icon should automatically show disabled when any parent in the containment hierarchy is disabled - this would be helpful where the workflow is large and has many nested SelectiveExecution instances. When you enable the parent, you would also probably want the child to revert back to its original state before the parent was disabled. I quite like that idea but I'll leave that as an exercise for the reader, or perhaps v2 someday. Also, no validation errors are showing for the "SelectiveExecution 3" activity even though the text is set to the literal value of 123 which would otherwise cause a validation exception to be shown (since you can't implicitly cast a literal integer to a string). Click the button to enable the activity and you'll see the errors.

Written by Christopher Owczarek