具有 OutArgument 和分配活动的自定义 WF4 活动

发布于 2024-10-05 00:25:33 字数 863 浏览 0 评论 0原文

我正在尝试通过编写标准活动来编写自定义活动,其中之一是分配活动,它负责将字符串值分配给名为“TextOut”的 OutArgument,我已在自定义活动中定义了该值。这个想法是,使用此自定义活动的工作流作者在工作流中定义一个变量,并将其映射到我的自定义活动的 TextOut OutArgument。我想通过迭代方法来实现这一点,因为我需要在运行时动态创建选择分支。我省略了这段代码以简化我的问题。

该活动的代码如下所示。这可能不是应该这样做的,因为它不起作用:)使用此活动的工作流程会引发验证错误:“未提供所需活动参数‘To’的值”。

我想获得一些有关如何让我的 OutArgument 与分配子活动一起工作的建议(因此无需在我的 OutArgument 上调用 .Set)。

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign assign = new Assign {
            To = this.TextOut,
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}

I am trying to write a custom activity by composing standard Activities, one of which is an Assign activity which is responsible for assigning a string value to an OutArgument, called 'TextOut', which I have defined on my custom Activity. The idea is that the workflow author that uses this custom Activity defines a variable in the Workflow and maps this to the TextOut OutArgument of my custom Activity. I'd like to achieve this with an iterative approach, since I have a requirement to dynamically create pick branches at runtime. I have left out this code to simplify my question.

The code for the Activity is shown below. This is probably not how it should be done, since it doesn't work :) The workflow which uses this Activity throws a validation error: "Value for a required activity argument 'To' was not supplied".

I'd like to get some advice on how to get my OutArgument working with an Assign child activity (so without calling .Set on my OutArgument).

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign assign = new Assign {
            To = this.TextOut,
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼波传意 2024-10-12 00:25:33

尝试在您的分配活动中使用 ArgumentReference,如下所示:

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign<string> assign = new Assign<string>
        {
            To = new ArgumentReference<string>("TextOut"),
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}

Try using a ArgumentReference in your Assign activity like this:

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign<string> assign = new Assign<string>
        {
            To = new ArgumentReference<string>("TextOut"),
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}
无人问我粥可暖 2024-10-12 00:25:33

如果你不想使用魔法字符串,你可以这样做。

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign<string> assign = new Assign<string>
        {
            To = new OutArgument<string>(ctx => TextOut.Get(ctx)),
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}

这似乎违反直觉,但由于 OutArgument<> 构造函数的参数是一个 Expression,因此可以将其转换为位置引用。这正是所发生的情况。

If you don't want to use magic strings you can do it this way.

public sealed class OutArgActivity : Activity
{
    public OutArgument<string> TextOut { get; set; }

    public OutArgActivity()
    {
        Assign<string> assign = new Assign<string>
        {
            To = new OutArgument<string>(ctx => TextOut.Get(ctx)),
            Value = new InArgument<string>(
                env => "this is my custom return value")
        };

        Sequence sequence = new Sequence();
        sequence.Activities.Add(assign);

        this.Implementation = () => sequence;
    }
}

This seems counter intuitive, but since the argument to the constructor of OutArgument<> is an Expression it is possible to convert it to a location reference. And that is exactly what happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文