Dynamically Modifying a Workflow from the Inside
Updating a workflow on runtime can be a case especially when you want to avoid rebuilding a workflow.
There are two available options to update an activity on runtime: from the inside or from the outside. In this post I describe first option and second option should be the topic of one of future posts.
First I create a simple workflow for my sample. It's nothing more than a Sequential Workflow Console Application with one workflow as you see:
This workflow consists of a Delay activity where application waits for a second then sets the title of console window and writes something in console. I just used it because love to use Delay activities!
private void delayActivity1_InitializeTimeoutDuration(object sender, EventArgs e)
{
Console.Title = "Dynamically Modifying a Workflow from the Inside";
Console.WriteLine("Delay ...");
}
There is also a Code activity in this sequential workflow. When executes, it writes its Name in console and dynamically adds a new Code activity to sequential workflow. This dynamically added Code activity writes its Name in console.
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);
}
In codeActivity1_ExecuteCode event, I follow a process to add a new Code activity to workflow:
First I create an instance of WorkflowChanges object by passing my root activity to it then create an instance of CodeActivity object, set its Name property and add its ExecuteCode event handler. After this I add this Code Activity to my WorkflowChanges object by calling Add() method of TransientWorkflow.Activities. TransientWorkflow is a property of WorkflowChanges class and keeps all required information about activities in its workflow. The last step is to call ApplyWorkflowChanges() method to save changes into WorkflowChanges object.
codeActivity2_ExecuteCode is a simple event handler:
private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{
CodeActivity codeActivity = sender as CodeActivity;
Console.WriteLine(codeActivity.Name);
Console.ReadLine();
}
The output shows that I could add my second Code Activity to workflow successfully.

Using activities in TransientWorkflow property, it's possible to edit or remove activities in a workflow at runtime.
[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.
5 Comments : 12.01.06

#1
vikram
12.01.2006 @ 9:34 PM