다음을 통해 공유


Using reference-type arguments in a workflow (WF)

Prior to the release candidate for .Net Framework v4, it was allowed to use arguments of a reference type with no wrapping or transformation. However, this led to some confusion concerning how reference types were passed to multiple instances of a workflow, so in the release version of .Net 4, some additional marshalling is needed, requiring the use of either LambdaValue<> or VisualBasicValue<> to pass the argument into the workflow.

For a list of changes between Beta 2 and RC for Windows Workflow Foundation, see the following entry:

http://blogs.msdn.com/b/endpoint/archive/2010/02/10/4-0-beta2-rc-wcf-wf-breaking-changes.aspx

In the following example, a TextBoxWriter (a custom implementation of System.IO.TextWriter that writes to a TextBox control on a form) is passed into a workflow to be used as the output stream for a WriteLine activity. A LambdaValue<> activity is used to assign the reference object to the activity; it is also used to pass the reference type into the workflow in the first place.

Hosting code:

    var workflow = new MyWorkflow();            
    workflow.Writer = new LambdaValue<TextWriter>(context=>new TextBoxWriter(textBox1));
    WorkflowInvoker.Invoke(workflow);

Workflow code:

    public class MyWorkflow : Activity
    {
        public InArgument<TextWriter> Writer { get; set; }
       
        public MyWorkflow()
        {
            this.Implementation = () => new WriteLine()
            {
                Text = "Hello World",
                TextWriter = new LambdaValue<TextWriter>(context => Writer.Get(context)) 
            };          
        }
    }