Passing arguments to activities scheduled by NativeActivity (WF)

When scheduling activities using the NativeActivityContext.ScheduleActivity method, it isn't immediately obvious how to flow arguments into that activity. Since the scheduled activity doesn't have access to the parent activity's arguments (as a child activity would), it's necessary to use a Variable to transmit the data.

The following code snippet demonstrates how to pass an argument from a native activity into a scheduled activity.

public sealed class ChildActivity : NativeActivity
    {
        public WriteLine _writeLine;
        public InArgument<string> Message { get; set; }
        private Variable<string> MessageVariable { get; set; }

        public ChildActivity()
        {
            MessageVariable = new Variable<string>();
            _writeLine = new WriteLine
                {
                    Text = new InArgument<string>(MessageVariable),
                };
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            base.CacheMetadata(metadata);
            metadata.AddImplementationVariable(this.MessageVariable);
            metadata.AddImplementationChild(this._writeLine);
        }

        protected override void Execute(NativeActivityContext context)
        {
            string configuredMessage = context.GetValue(Message);
            context.SetValue(MessageVariable, configuredMessage);
            context.ScheduleActivity(this._writeLine);
        }
    }

The issue is discussed in more detail in the following blog post:

http://blogs.msdn.com/b/tilovell/archive/2010/02/26/misadventures-in-cachemetadata-wrapping-an-inner-activity-in-code.aspx