Workflow Activities should be referred by sender object or e.Activity, not by activity property name

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

.Net Remoting clent proxy error in debugger inspection

I am using .Net Remoting and tried to debug call from client proxy. It didn’t allow me to step into(whick is actually normal) and Debugger inspection showed me that Identity property has an exception:
 

  Identity System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.Runtime.Remoting.RemotingException: Permission denied: cannot call non-public or static methods remotely.

Server stack trace:
   at System.Runtime.Remoting.Channels.ChannelServices.DispatchMessage(IServerChannelSinkStack sinkStack, IMessage msg, IMessage& replyMsg)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.MarshalByRefObject.get_Identity()
   — End of inner exception stack trace —
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at Microsoft.Office.Tools.Debugger.Tools.TryCreateDebuggerItem(MemberInfo member, Object target, __Item& item) System.Reflection.TargetInvocationException

 
I’ve decided that there is an error and tried to find the solution. Similar issues (without good explanation) are:
 
Fortunately this debugger message can be ignored, and my call to remoting service worked as expected.