WF4RC, WriteLine activity throws error on StringWriter assigned to TextWriter

好久不见. 提交于 2019-12-05 09:35:06

Ack, k, so mental note: Attempt all permutations of a Google search. Found this after searching for

WriteLine activity WF RC

The solution is to wrap it in a LambdaValue<T>, comme ca

[TestMethod]
public void TestMethod ()
{
    StringWriter writer = new StringWriter ();
    Sequence sequence = new Sequence
    {
        Activities =
        {
            new WriteLine 
            {
                Text = "Hello", 
                TextWriter = new LambdaValue<TextWriter> (n => writer) 
            },
            new WriteLine 
            {
                Text = "World", 
                TextWriter = new LambdaValue<TextWriter> (n => writer) 
            }
        }
    };
    WorkflowInvoker.Invoke (sequence);
    Assert.
        AreEqual (
        "Hello\r\nWorld\r\n", 
        writer.GetStringBuilder ().ToString ());
}

which seems weird to me, but I am literally the opposite of "expert" :P

I would still welcome alternatives if anyone has them.

just stuck over it as well ... here my humble opinion

TextWriter is an InArgument like any other element of the activity (e.g. the Text element). An InArgument is calculated in context of the workflow (so consequently uses the ActivityContext to gather the real value in this workflow).

As you assign the writer directly, it is automatically converted to an InArgument with a Literal expression behind it. Literals are more or less constant parts of a workflow and are therefore not allowed to change. The exception tells you to avoid using a type which state would change.

Using the LambdaValue expression activity allows you to assign in each instantiation of the workflow a (new) writer. The workflow expect this object to be of temporary nature till the workflow is over.

I hope this clarify this issue and I have not made myself a moroon.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!