Some thought on saving state in a custom activity in a state workflow.
Posted
Thursday, December 14, 2006 11:42 AM
by
Maurice
I have been a little frustrated in being unable to find a good solution for the following problem. If anyone has a good suggestion the feedback would be appreciated greatly.
Suppose I have a custom activity named CustomEventActivity that implements IEventActivity. The activity is used in a state workflow. This activity receives an event containing some data and saves this data so a second activity in the same EventDrivenActivity can check on the data. Something like:
protected override ActivityExecutionStatus
Execute(ActivityExecutionContext executionContext)
{
WorkflowQueuingService wqs =
executionContext.GetService<WorkflowQueuingService>();
WorkflowQueue queue = wqs.GetWorkflowQueue(QueueName);
MyProperty = queue.Dequeue() as string;
return ActivityExecutionStatus.Closed;
}
Now I need to check on the data saved in MyProperty in a second activity in the same EventDrivenActivity. Beecause of the spawned context I can’t just use this.myCustomActivity1.MyProperty as this just returns the template activity with an empty property.
In a CodeActivity I can do something like:
EventDrivenActivity eventDriven;
CodeActivity code = sender as Activity;
eventDriven = code.Parent;
CustomEventActivity act1 = eventDriven.GetActivityByName(myCustomActivity1.QualifiedName, true) as CustomEventActivity;
Now this is cumbersome and error prone so I don’t like it 
But things get worse if you want to use the value in a Declarative Rule Condition. Now it needs to be a single expression and we are located a little further inside the activity tree. I would need to use: ((CustomEventActivity)((Activity)sender).Parent.Parent.Parent.GetActivityByName(myCustomActivity1.QualifiedName, true)).MyProperty
Big yikes 
Now I could just add a single property to the workflow and bind to that but in that case I would need to define extra properties on the workflow level that really have no purpose being there. Two additional problems:
- If I want to use XAML workflows I would need to define the properties in base class, making it unusable when users want to expand the XAML workflow in a rehosted designer.
- It is no longer possible to use a ReplicatorActity with ExecutionType is parallel. After all each parallel executing branch would point to the same activity.