Control the Modifiability of a Workflow

Back in December 2006, I wrote two posts about dynamically modifying a workflow from the inside and dynamically modifying a workflow from the outside.  They're descriptive enough to show how to modify a workflow with either ways but what will you do if you want to prevent a workflow from being modified?  In this post, I'll show you how to disable modifiability of a workflow.

To demonstrate how to control the modifiability of a workflow, I use the sample workflow that I wrote to show how to modify a workflow from the inside in the past.

Sample Workflow

In that workflow I have a delay activity and a code activity in a sequential workflow.  After a short delay, my code activity executes a code and creates another code activity on fly and adds it to currently running workflow to generate an output in console.

private void delayActivity1_InitializeTimeoutDuration(object sender, EventArgs e)

{

    Console.Title = "Control the Modifiability of a Workflow";

    Console.WriteLine("Delay ...");

}

 

private void codeActivity1_ExecuteCode(object sender, EventArgs e)

{

    CodeActivity delay = sender as CodeActivity;

    Console.WriteLine(delay.Name);

 

    WorkflowChanges workflowChanges = new WorkflowChanges(this);

 

    CodeActivity codeActivity = new CodeActivity();

    codeActivity.Name = "codeActivity2";

    codeActivity.ExecuteCode += new EventHandler(codeActivity2_ExecuteCode);

 

    workflowChanges.TransientWorkflow.Activities.Add(codeActivity);

    this.ApplyWorkflowChanges(workflowChanges);

}

 

private void codeActivity2_ExecuteCode(object sender, EventArgs e)

{

    CodeActivity codeActivity = sender as CodeActivity;

 

    Console.WriteLine(codeActivity.Name);

    Console.ReadLine();

}

Output

Every workflow has a DynamicUpdateCondition property which lets you to enable or disable the modifiability of that workflow.  By default workflows are modifiable but to change this behavior you can set a condition for workflow.  There are two types of conditions that you can set for a workflow:

You can set the value of DynamicUpdateCondition property in Properties window to one of these two values and set other related values for them based on your choice.

Properties Window

First I use a Code Condition to disable my workflow's modifiability if the TimeoutDuration of my delay activity is more than five seconds.  My Code Condition method (which is named UpdateCondition) has two parameters and returns nothing.  First parameter is an object and second one is of ConditionalEventArgs type.  This latter parameter has a Result boolean property.  If you set it to false then workflow isn't modifiable and any attempt to modify it throws an exception on runtime and if you set it to true then workflow is modifiable.  Here is the simple logic for my code condition:

private void UpdateCondition(object sender, ConditionalEventArgs e)

{

    if (TimeSpan.Compare(this.delayActivity1.TimeoutDuration, new TimeSpan(0, 0, 5)) > 0)

    {

        e.Result = false;

    }

    else

    {

        e.Result = true;

    }

}

So if I set my TimoutDuration to ten seconds then will get an exception on runtime as you see:

Exception

Similarly I could use a Declarative Rule Condition.  In this case, workflow is modifiable until an expression is validated as true otherwise it will fail to modify.  For example, I create a condition to check if the number of activities in current workflow is zero then make it modifiable.

Rule Condition Editor

Properties Window

In my case workflow is not modifiable because the number of activities is not zero.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

1 Comment : 06.18.07

Feedbacks

 avatar
#1
spitraberg
12.06.2007 @ 5:42 AM
hi , after l want to change my workflow (asp.net)[in remove action],ValidationError with message below(a) happend and if ignore ValidationError for changing workflow it's done correctly[(RemovedActivity Table contains removed activity:)] but when want to load instance activities this error happend(b) : (a):"Activity 'cancellationHandlerActivity1' validation warning: Empty behavior activity 'cancellationHandlerActivity1' of type System.Workflow.ComponentModel.CancellationHandlerActivity found, this might override default behavior from execution, check whether this is intended." (b): A field or property with the name 'AddedActivityAction' was not found on the selected data source."

Leave a Comment