Search code examples
c#workflowworkflow-foundation-4workflow-activity

How to execute ActivityBuilder without serializing it?


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?


Solution

  • 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);