如何在不序列化的情况下执行ActivityBuilder?

发布于 2025-01-05 06:39:16 字数 1374 浏览 1 评论 0原文

假设我有一个像这样以编程方式创建的工作流,

ActivityBuilder<int> ab = new ActivityBuilder<int>();
ab.Name = "Add";
ab.Properties.Add(new DynamicActivityProperty {Name = "Operand1", Type = typeof (InArgument<int>)});
ab.Properties.Add(new DynamicActivityProperty {Name = "Operand2", Type = typeof (InArgument<int>)});
ab.Implementation = new Sequence
        {
            Activities =
                {
                    new WriteLine
                        {
                            Text =
                                new VisualBasicValue<string>(
                                "Operand1.ToString() + \" + \" + Operand2.ToString()")
                        }

                }
        };

我知道执行它的一种方法是首先将 ActivityBuilder 对象序列化为 XAML。接下来,使用 ActivityXamlServices 加载序列化的 XAML。创建参数字典。使用 WorkflowInvokerWorkflowApplication 执行它

有没有办法执行此工作流而不需要将活动生成器转换/序列化为 XAML?

WorkflowApplication 和 WorkflowInvoker 将 Activity 作为执行的输入。我可以以某种方式直接将 activityBuilder.ImplementationWorkflowApplicationWorkflowInvoker 结合使用吗?

为什么我想要这个?因为我们有一个工作流设计器,用户可以使用它来创建和执行工作流。用户还可以通过编程方式创建工作流程。工作流的大小最大可达 80MB。由于 80MB 文件与 XAML 之间的序列化和反序列化,这会损害应用程序的内存。我想以某种方式跳过这一步并直接执行活动。

有道理吗?

Let's say I have a workflow created progrmatically like this

ActivityBuilder<int> ab = new ActivityBuilder<int>();
ab.Name = "Add";
ab.Properties.Add(new DynamicActivityProperty {Name = "Operand1", Type = typeof (InArgument<int>)});
ab.Properties.Add(new DynamicActivityProperty {Name = "Operand2", Type = typeof (InArgument<int>)});
ab.Implementation = new Sequence
        {
            Activities =
                {
                    new WriteLine
                        {
                            Text =
                                new VisualBasicValue<string>(
                                "Operand1.ToString() + \" + \" + Operand2.ToString()")
                        }

                }
        };

One way I know to execute it is to first serialize the ActivityBuilder object into XAML. Next, load the serialized XAML using ActivityXamlServices. Create a dictionary for parameters. Execute it using WorkflowInvoker or WorkflowApplication

Is there any way to execute this workflow without the need to convert/serialize activity builder to XAML?

WorkflowApplication and WorkflowInvoker takes an Activity as input for execution. Can I somehow use activityBuilder.Implementation directly with WorkflowApplication or WorkflowInvoker?

Why I want this? Because we have a workflow designer which user uses to create and execute workflow. User also creates workflow progrmatically. Workflow can be up to 80MB in size. This is hurting application's memory due to serialization and de-serialization of 80MB files to and from XAML. I want to somehow skip this step and directly execute activity.

Does it makes sense?

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

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

发布评论

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

评论(1

十雾 2025-01-12 06:39:16

无需使用 ActivityBuilder,只需创建所需的活动并执行它们即可。

var wf = new Sequence()
{
    Variables =
    {
        new Variable<int>("Operand1", 7),
        new Variable<int>("Operand2", 42)
    },
    Activities =
    {
        new WriteLine
        {
            Text =
                new VisualBasicValue<string>(
                "Operand1 & \" + \" & Operand2")
        }
    }
};


WorkflowInvoker.Invoke(wf);

使用 DynamicActivityProperty 的示例:

    var wf = new DynamicActivity<int>
    {
        Properties =
         {
            new DynamicActivityProperty { Name = "Operand1", Type = typeof(InArgument<int>) },
            new DynamicActivityProperty { Name = "Operand2", Type = typeof(InArgument<int>) }
         },
        Implementation = () => new Sequence()
            {
                Activities =
                {
                    new WriteLine
                    {
                        Text =
                            new VisualBasicValue<string>(
                            "Operand1 & \" + \" & Operand2")
                    },
                    new Assign<int>
                    {
                        To = new ArgumentReference<int> { ArgumentName = "Result" },
                        Value = new VisualBasicValue<int>("Operand1 + Operand2")
                    }
                }
            }
    };

    var inputs = new Dictionary<string, object>();
    inputs["Operand1"] = 7;
    inputs["Operand2"] = 42;
    var output = WorkflowInvoker.Invoke(wf, inputs);
    Console.WriteLine(output);

No need to use an ActivityBuilder, just create the activities you want and execute them.

var wf = new Sequence()
{
    Variables =
    {
        new Variable<int>("Operand1", 7),
        new Variable<int>("Operand2", 42)
    },
    Activities =
    {
        new WriteLine
        {
            Text =
                new VisualBasicValue<string>(
                "Operand1 & \" + \" & Operand2")
        }
    }
};


WorkflowInvoker.Invoke(wf);

An example using DynamicActivityProperty:

    var wf = new DynamicActivity<int>
    {
        Properties =
         {
            new DynamicActivityProperty { Name = "Operand1", Type = typeof(InArgument<int>) },
            new DynamicActivityProperty { Name = "Operand2", Type = typeof(InArgument<int>) }
         },
        Implementation = () => new Sequence()
            {
                Activities =
                {
                    new WriteLine
                    {
                        Text =
                            new VisualBasicValue<string>(
                            "Operand1 & \" + \" & Operand2")
                    },
                    new Assign<int>
                    {
                        To = new ArgumentReference<int> { ArgumentName = "Result" },
                        Value = new VisualBasicValue<int>("Operand1 + Operand2")
                    }
                }
            }
    };

    var inputs = new Dictionary<string, object>();
    inputs["Operand1"] = 7;
    inputs["Operand2"] = 42;
    var output = WorkflowInvoker.Invoke(wf, inputs);
    Console.WriteLine(output);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文