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.
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).