子活动的自定义活动设置参数

发布于 2024-10-30 16:58:20 字数 1254 浏览 3 评论 0 原文

我正在尝试为 WF4 创建一个自定义活动,该活动托管子活动并将一些参数传递给其子活动。我在下面附上了我的活动的简化版本(父活动和子活动),

public class Child : CodeActivity
{
    public InArgument<Dictionary<string, object>> Data;

    protected override void Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        //Some operations on the input data
    }
}


 public class Parent : NativeActivity
{
    public InArgument<int> Value1 { get; set; }

    public InArgument<string> Value2 { get; set; }

    public Child Body { get; set; }


    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        context.SetValue(Body.Data, data);

        context.ScheduleActivity(this.Body);
    }


    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        Body = new Child();

        base.CacheMetadata(metadata);
    }
}

当工作流执行到达活动的 Execute 方法时,子活动的 Data 参数为 null。

有人可以给我一些指导,如何在这两项活动之间传递争论吗?

I am trying to create a custom activity for WF4 that host a child activity and pass some arguments to its child activity. I attach below a simplified version of my activities (Parent and Child)

public class Child : CodeActivity
{
    public InArgument<Dictionary<string, object>> Data;

    protected override void Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        //Some operations on the input data
    }
}


 public class Parent : NativeActivity
{
    public InArgument<int> Value1 { get; set; }

    public InArgument<string> Value2 { get; set; }

    public Child Body { get; set; }


    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        context.SetValue(Body.Data, data);

        context.ScheduleActivity(this.Body);
    }


    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        Body = new Child();

        base.CacheMetadata(metadata);
    }
}

The Data argument from the Child activity is null when the workflow execution reaches the Execute method for the activity.

Can someone please, give me some direction how could the arguments be passed between this two activities?

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

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

发布评论

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

评论(3

笨死的猪 2024-11-06 16:58:20

虽然使用额外变量的方法有效,但在 ActivityAcytion 中使用调度具有某些输入的子级的“官方”方法,或者如果您还希望使用 ActivityFunc 获得结果。

恐怕代码并没有真正变得更简单或更容易理解,但为了完整性,我决定添加这个。

public class Child : CodeActivity<object>
{
    public InArgument<Dictionary<string, object>> Data { get; set; }

    protected override object Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        foreach (var item in data)
        {
            Console.WriteLine(item);
        }

        return "Some result";
    }
}



public class Parent : NativeActivity<object>
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }

    public ActivityFunc<Dictionary<string, object>, object> Body { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        context.ScheduleFunc<Dictionary<string, object>, object>(Body, data, ChildCompletionCallback);
    }


    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        var arg = new DelegateInArgument<Dictionary<string, object>>();

        Body = new ActivityFunc<Dictionary<string, object>, object>
        {
            Argument = arg,
            Handler = new Child() { Data = arg }
        };

        base.CacheMetadata(metadata);
    }

    private void ChildCompletionCallback(NativeActivityContext context, ActivityInstance completedInstance, object result)
    {
        //Set the output for the parent activity at the completion of the child activity
        this.Result.Set(context, result);
    }
}

While the approach with the extra variable works the "official" way of scheduling a child with some inputs is using in ActivityAcytion or if you also want a result with an ActivityFunc.

I am afraid the code doesn't really get to be any simpler or easier to understand but for completeness sake I just decided to add this.

public class Child : CodeActivity<object>
{
    public InArgument<Dictionary<string, object>> Data { get; set; }

    protected override object Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        foreach (var item in data)
        {
            Console.WriteLine(item);
        }

        return "Some result";
    }
}



public class Parent : NativeActivity<object>
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }

    public ActivityFunc<Dictionary<string, object>, object> Body { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        context.ScheduleFunc<Dictionary<string, object>, object>(Body, data, ChildCompletionCallback);
    }


    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        var arg = new DelegateInArgument<Dictionary<string, object>>();

        Body = new ActivityFunc<Dictionary<string, object>, object>
        {
            Argument = arg,
            Handler = new Child() { Data = arg }
        };

        base.CacheMetadata(metadata);
    }

    private void ChildCompletionCallback(NativeActivityContext context, ActivityInstance completedInstance, object result)
    {
        //Set the output for the parent activity at the completion of the child activity
        this.Result.Set(context, result);
    }
}
听不够的曲调 2024-11-06 16:58:20

我还需要通过父活动返回子活动的结果。为此,我使用了 ScheduleActivity 方法和 CompletionCallback 参数。我在下面附上了包含 out 参数集的父/子示例活动,也许某个时候有人会需要它:)

父:

public class Parent : NativeActivity<object>
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }
    private Variable<Dictionary<string, object>> SomeVariable { get; set; } // intermediate variable

    private Child Body { get; set; }

    public Parent()
    {
        this.SomeVariable = new Variable<Dictionary<string, object>>("SomeVariable");

        this.Body = new Child();
        this.Body.Data = new InArgument<Dictionary<string, object>>(SomeVariable);
    }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        this.SomeVariable.Set(context, data);

        context.ScheduleActivity(this.Body, ChildCompletionCallback);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        // needs to be cached as implementation child and variable (Body and SomeVariable must be declared as private then)
        metadata.AddImplementationChild(this.Body);
        metadata.AddImplementationVariable(SomeVariable);
    }

    private void ChildCompletionCallback<TResult>(NativeActivityContext context, ActivityInstance completedInstance, TResult result)
    {
        //Set the output for the parent activity at the completion of the child activity
        this.Result.Set(context, result);
    }
}

子:

public class Child : CodeActivity<object>
{
    public InArgument<Dictionary<string, object>> Data { get; set; }

    protected override object Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        return "Some result";
    }
}

I also needed to return the result from the child activity via the parent activity. For this I used the ScheduleActivity method with the CompletionCallback parameter. I attached below the Parent / Child example activities containing the out argument set also, maybe it will be needed by someone at some time :)

Parent:

public class Parent : NativeActivity<object>
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }
    private Variable<Dictionary<string, object>> SomeVariable { get; set; } // intermediate variable

    private Child Body { get; set; }

    public Parent()
    {
        this.SomeVariable = new Variable<Dictionary<string, object>>("SomeVariable");

        this.Body = new Child();
        this.Body.Data = new InArgument<Dictionary<string, object>>(SomeVariable);
    }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        this.SomeVariable.Set(context, data);

        context.ScheduleActivity(this.Body, ChildCompletionCallback);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        // needs to be cached as implementation child and variable (Body and SomeVariable must be declared as private then)
        metadata.AddImplementationChild(this.Body);
        metadata.AddImplementationVariable(SomeVariable);
    }

    private void ChildCompletionCallback<TResult>(NativeActivityContext context, ActivityInstance completedInstance, TResult result)
    {
        //Set the output for the parent activity at the completion of the child activity
        this.Result.Set(context, result);
    }
}

Child:

public class Child : CodeActivity<object>
{
    public InArgument<Dictionary<string, object>> Data { get; set; }

    protected override object Execute(CodeActivityContext context)
    {
        Dictionary<string, object> data = Data.Get(context);

        return "Some result";
    }
}
浅黛梨妆こ 2024-11-06 16:58:20

可能无法直接在 Execute 方法中设置输入参数的值。必须引入一个中间变量。 Child.Data 参数连接到此变量并在 Parent.Execute 方法中。

家长:

public class Parent : NativeActivity
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }
    private Variable<Dictionary<string, object>> SomeVariable { get; set; } // intermediate variable

    private Child Body { get; set; }

    public Parent()
    {
        this.SomeVariable = new Variable<Dictionary<string, object>>("SomeVariable");

        this.Body = new Child();
        this.Body.Data = new InArgument<Dictionary<string, object>>(SomeVariable);
    }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        this.SomeVariable.Set(context, data);

        context.ScheduleActivity(this.Body);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        // needs to be cached as implementation child and variable (Body and SomeVariable must be declared as private then)
        metadata.AddImplementationChild(this.Body);
        metadata.AddImplementationVariable(SomeVariable);
    }
}

孩子:

public class Child : CodeActivity
{
    // Must be declared as property.
    public InArgument<Dictionary<string, object>> Data
    {
        get;
        set;
    }

    protected override void Execute(CodeActivityContext context)
    {
        var data = this.Data.Get(context);
    }
}

这不是一个好的解决方案,但我在 WF4 中几乎看不到任何好的东西:)。

it is probably not possible to set value of input argument directly in Execute method. An intermediate variable must be introduced. Child.Data argument is connected to this variable and in Parent.Execute method.

Parent:

public class Parent : NativeActivity
{
    public InArgument<int> Value1 { get; set; }
    public InArgument<string> Value2 { get; set; }
    private Variable<Dictionary<string, object>> SomeVariable { get; set; } // intermediate variable

    private Child Body { get; set; }

    public Parent()
    {
        this.SomeVariable = new Variable<Dictionary<string, object>>("SomeVariable");

        this.Body = new Child();
        this.Body.Data = new InArgument<Dictionary<string, object>>(SomeVariable);
    }

    protected override void Execute(NativeActivityContext context)
    {
        int value1 = Value1.Get(context);
        string value2 = Value2.Get(context);

        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("value1", value1);
        data.Add("value2", value2);

        this.SomeVariable.Set(context, data);

        context.ScheduleActivity(this.Body);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        // needs to be cached as implementation child and variable (Body and SomeVariable must be declared as private then)
        metadata.AddImplementationChild(this.Body);
        metadata.AddImplementationVariable(SomeVariable);
    }
}

Child:

public class Child : CodeActivity
{
    // Must be declared as property.
    public InArgument<Dictionary<string, object>> Data
    {
        get;
        set;
    }

    protected override void Execute(CodeActivityContext context)
    {
        var data = this.Data.Get(context);
    }
}

This is not a nice solution but I hardly see anything nice in WF4:).

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