如何在Microsoft工作流中的另一个活动中调用活动? [英] How to call an Activity inside another Activity in Microsoft Workflow?

查看:93
本文介绍了如何在Microsoft工作流中的另一个活动中调用活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个本机活动,我需要在书签恢复调用中调用另一个活动。我的第一个活动名称是 Apply,第二个活动称为 Approve。在应用中,我创建了一个如下属性。

I have created a Native activity and I need to call another activity on a Bookmark Resume call. My First Activity name is "Apply" and Second activity is called "Approve". In Apply, i have created a property as below.

  public Approve Approve
    {
        get;
        set;
    }

然后,我注册了CacheMetadata如下。

and then I have registered the CacheMetadata as below.

        metadata.AddImplementationChild(this.Approve);
        base.CacheMetadata(metadata);

然后使用 OnResumeBookmark方法,对它进行调度。

And then "OnResumeBookmark" method, Im scheduling it.

        this.Approve = new Approve();
        this.Approve.ID = context.GetValue(this.ID);
        OutArgument<string> res = this.Approve.Result;
        context.ScheduleActivity(this.Approve);

但是当它运行时,会出现以下错误。

But When it runs, it gives me the below error.

在处理元数据时,提供的活动不属于此工作流程定义。名为批准的问题活动由名为应用的活动提供。

您能帮我解决这个问题吗?

Could you please help me to resolve this?

推荐答案

有两种开发活动的方法:

There are two approaches to activity development:


  • 私人实施路线

  • 公共实施路线

似乎正在根据您的问题制定私有实施策略,但我将在下面提供两种方式的示例代码。

It appears you are working on the private implementation strategy from your question, but I'll provide sample code for both ways below.


私有实现方式

The Private Implementation Way

在这种方法中, Apply 活动是负责设置有关批准子活动的所有内容。我们使用WF Variable< string> 对象连接 ID 属性。另外,我们通知WF运行时,可以通过 Apply 活动安排 Approve 活动,就像您已经

In this approach, the Apply activity is responsible for setting up everything about the Approve child activity. We wire up the ID property using the WF Variable<string> object. Also, we inform the WF run time that the Approve activity can be scheduled by the Apply activity, just like you have in your code sample.

public class Approve : CodeActivity
{
    public InArgument<string> Something
    {
        get; set;
    }

    protected override void Execute(CodeActivityContext context)
    {
        var message = string.Format("Approval of {0} in process... please wait.", this.Something.Get(context));
        Console.WriteLine(message);
    }
}

public class Apply : NativeActivity
{
    private readonly Approve approve;
    private readonly Variable<string> thingToApproveVar;

    public Apply()
    {
        this.thingToApproveVar = new Variable<string>();
        this.approve = new Approve();
        this.approve.Something = new InArgument<string>(this.thingToApproveVar);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddImplementationVariable(this.thingToApproveVar);
        metadata.AddImplementationChild(this.approve);
    }

    protected override void Execute(NativeActivityContext context)
    {
        Console.WriteLine("Approving...");
        this.thingToApproveVar.Set(context, "Hello");
        context.ScheduleActivity(this.approve, this.OnApprovalCompleted);
    }

    private void OnApprovalCompleted(NativeActivityContext activityContext, ActivityInstance instance)
    {
        Console.WriteLine("Apply succeeded!");
    }
}




公共实施方式

The Public Implementation Way

开发活动的第二种方法是将 Apply 活动定义为只需安排批准活动。但是,通过使用 AddChild 方法而不是 CacheMetadata 方法对此需要进行一些微调。 > AddImplementationChild 方法。

The second approach in developing your activity is to define your Apply activity to simply schedule the Approve activity. The CacheMetadata method needs a slight tweak for this though, by using the AddChild method instead of the AddImplementationChild method.

public class Apply : NativeActivity
{
    public Apply()
    {
    }

    public Approve Approve { get; set; }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddChild(this.Approve);
    }

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(this.Approve, this.OnApprovalCompleted);
    }

    private void OnApprovalCompleted(NativeActivityContext activityContext, ActivityInstance instance)
    {
        Console.WriteLine("Apply succeeded!");
    }
}

并执行公共实施方式,如下所示:

And to execute the "public implementation way", it would look like the following:

class Program
{
    static void Main(string[] args)
    {
        WorkflowInvoker.Invoke(new Sequence()
        {
            Activities =
            {
                new Apply()
                {
                    Approve = new Approve()
                    {
                        Something = "Hello World!"
                    }
                }
            }
        });
    }
}

这篇关于如何在Microsoft工作流中的另一个活动中调用活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆