How to call an Activity inside another Activity in Microsoft Workflow?

拜拜、爱过 提交于 2019-12-01 14:57:25

There are two approaches to activity development:

  • The Private Implementation Route
  • The Public Implementation Route

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

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

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