5

am new to Windows Workflow [WF], and interested in evaluating WF for business purposes. I decided to work through an introduction

[TestMethod]
public void TestMethod ()
{
    TextWriter writer = new StringWriter ();
    Sequence sequence = new Sequence
    {
        Activities =
        {
            // so, assigning a reference type [eg StringWriter]
            // as target is prohibited in WF4RC. what or how do
            // i assign a target? introduction cited above may
            // not be current [ie may be Beta2, not RC] so ... ?
            new WriteLine { Text = "Hello", TextWriter = writer },
            new WriteLine { Text = "World", TextWriter = writer }
        }
    };
    // !!! BLOWS UP !!!
    WorkflowInvoker.Invoke (sequence);
}

and encountered

Test method SomeTests.SomeTests.TestMethod threw exception: System.Activities.InvalidWorkflowException: The following errors were encountered while processing the workflow tree: 'Literal': Literal only supports value types and the immutable type System.String. The type System.IO.TextWriter cannot be used as a literal.

Poking around, I found this article describing what appears to be the error I see above.

Being new to WF, I do not really understand the change or prescribed method to work around it. So, my question is,

With WF4RC, how does one [correctly] use WriteLine activity?

4

2 回答 2

8

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.

于 2010-02-19T19:45:18.043 回答
0

只是卡在它上面......这是我的拙见

TextWriter 与活动的任何其他元素(例如 Text 元素)一样,是一个 InArgument。InArgument 在工作流的上下文中计算(因此使用 ActivityContext 来收集此工作流中的实际值)。

当您直接分配 writer 时,它会自动转换为 InArgument,后面带有 Literal 表达式。文字或多或少是工作流程中不变的部分,因此不允许更改。该异常告诉您避免使用状态会改变的类型。

使用 LambdaValue 表达式活动,您可以在工作流的每个实例中分配一个(新的)编写器。工作流期望此对象具有临时性质,直到工作流结束。

我希望这能澄清这个问题,我并没有让自己白痴。

于 2011-04-19T21:01:30.727 回答