Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
In Windows Workflow I had a Replicator Activity inside While Activity. In Replicator  _Initialized event handler I've set the InitialChildData property of the activity with the list of my objects, but Replicator  didn't invoke  any _ChildInitialized event , but went directly to _Completed event (similar problem was reported in the thread ReplicatorActivity shows all children complete when they aren't ). I tried to change a few things without success.It was also confusing that even in Initialized AllChildrenComplete property was true. I compared my code with exampe from excellent WF Tutorial: Using the Replicator for Windows Workflow Foundation, but didn't find any essential differences.
 
Finally WF Serialization Part One and a half article pointed me to Spawned Contexts - Replicator, While, State, EventHandlers, and CAG , that exlpained that I've incorrectly updated data of a template activity instead of instance activity.
Important extracts from  the article:
 
 If you want to avoid issues, learn to access activity properties in a context safe way (using the sender object or  e.Activity) so that you do it right when it counts. 
// WRONG CODE
this.delayActivity1.TimeoutDuration = TimeSpan.FromSeconds(iterationCount);
// RIGHT CODE
((DelayActivity)sender).TimeoutDuration = TimeSpan.FromSeconds(iterationCount);

// RIGHT CODE
CallExternalMethodActiivty act = e.Activity.GetActivityByName("createTask1", true) as CallExternalMethodActivity;
act.ParameterBindings["userName"].Value = e.InstanceData;

Use sender or e.Activity instead of this.  e.Activity is the clone of the replicator's template.  Second, we have passed the parameter true to GetActivityByName.  This tells the method to look only in the context of the activity on which it was called.  This keeps the method from walking into other parts of the tree and returning the RootContext instance.

 The issue also described in "Pro WF" By Bruce Bukovics access activities in a context safe way   section and in ActivityExecutionContext in Workflows article.

Note, that IsDynamicActivity Property can be used to determine was activity spawned or is executing within the default ActivityExecutionContext of the workflow instance.

 I hope that Microsoft will change the syntax of event handlers to separate workflow templates and instances to avoid these not-intuitive errors or at least will generate warnings if code is not context-safe(see my MS connect suggestion).


Posted on Tuesday, April 21, 2009 9:57 PM Workflow | Back to top


Comments on this post: Workflow Activities should be referred by sender object or e.Activity, not by activity property name

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net