InvokeWorkflow activity inside replicator activity

I have an invokeworkflow activity inside a replicator activity. The workflow I am trying to invoke requires two parameters to be passed, an integer and a string parameter, which must be passed to the workflow through a replicator action. Any ideas on how this can be done?

Thank.

0


source to share


3 answers


Here is a complete example (note that everything included in the designers can be set in the properties panel of the designer): Workflow3 is a target workflow that contains only CodeActivity, and the following code:

public sealed partial class Workflow3 : SequentialWorkflowActivity
{
    public static readonly DependencyProperty MyIntProperty =
        DependencyProperty.Register("MyInt", typeof(int), typeof(Workflow3));
    public static readonly DependencyProperty MyStringProperty =
        DependencyProperty.Register("MyString", typeof(string), typeof(Workflow3));

    public Workflow3()
    {
        InitializeComponent();

        this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode);
    }

    public int MyInt
    {
        get { return (int)GetValue(MyIntProperty); }
        set { SetValue(MyIntProperty, value); }
    }

    public string MyString
    {
        get { return (string)GetValue(MyStringProperty); }
        set { SetValue(MyStringProperty, value); }
    }

    private void codeActivity1_ExecuteCode(object sender, EventArgs e)
    {
        Console.WriteLine("Invoke WF: Int = {0}, String = {1}", this.MyInt, this.MyString);
    }
}

      

Workflow2 is a hosting workflow that contains only ReplicatorActivity. ReplicatorActivity contains only InvokeWorkflowActivity for which TargetWorkflow is set to Workflow3. The coding looks like this:

public sealed partial class Workflow2 : SequentialWorkflowActivity
{
    // Variables used in bindings
    public int InvokeWorkflowActivity1_MyInt = default(int);
    public string InvokeWorkflowActivity1_MyString = string.Empty;

    public Workflow2()
    {
        InitializeComponent();

        // Bind MyInt parameter of target workflow to my InvokeWorkflowActivity1_MyInt
        WorkflowParameterBinding wpb1 = new WorkflowParameterBinding("MyInt");
        wpb1.SetBinding(WorkflowParameterBinding.ValueProperty, new ActivityBind(this.GetType().Name, "InvokeWorkflowActivity1_MyInt"));
        this.invokeWorkflowActivity1.ParameterBindings.Add(wpb1);

        // Bind MyString parameter of target workflow to my InvokeWorkflowActivity1_MyString
        WorkflowParameterBinding wpb2 = new WorkflowParameterBinding("MyString");
        wpb2.SetBinding(WorkflowParameterBinding.ValueProperty, new ActivityBind(this.GetType().Name, "InvokeWorkflowActivity1_MyString"));
        this.invokeWorkflowActivity1.ParameterBindings.Add(wpb2);

        // Add event handler for Replicator Initialized event
        this.replicatorActivity1.Initialized += new EventHandler(ReplicatorInitialized);

        // Add event handler for Replicator ChildInitialized event
        this.replicatorActivity1.ChildInitialized += new EventHandler<ReplicatorChildEventArgs>(this.ChildInitialized);
    }

    private void ReplicatorInitialized(object sender, EventArgs e)
    {
        // Find how many workflows I want
        List<MyClass> list = new List<MyClass>();
        list.Add(new MyClass() { MyInt = 1, MyString = "Str1" });
        list.Add(new MyClass() { MyInt = 2, MyString = "Str2" });
        list.Add(new MyClass() { MyInt = 3, MyString = "Str3" });

        // Assign list to replicator
        replicatorActivity1.InitialChildData = list;
    }

    private void ChildInitialized(object sender, ReplicatorChildEventArgs e)
    {
        // This is the activity that is initialized
        InvokeWorkflowActivity currentActivity = (InvokeWorkflowActivity)e.Activity;

        // This is the initial data
        MyClass initialData = (MyClass)e.InstanceData;

        // Setting the initial data to the activity
        InvokeWorkflowActivity1_MyInt = initialData.MyInt;
        InvokeWorkflowActivity1_MyString = initialData.MyString;
    }

    public class MyClass
    {
        public int MyInt { get; set; }
        public string MyString { get; set; }
    }
}

      



The expected output is as follows:

Invoke WF: Int = 1, String = Str1
Invoke WF: Int = 2, String = Str2
Invoke WF: Int = 3, String = Str3

      

Hope it helps you.

+2


source


I realize this post is old, but for those who google this with the same question, this is what you need to do:

  • Wrap your call to InvokeWorkflow in a custom activity - this will make it easier to display parameters. In this exercise, create a DependencyProperty for each of the properties that you want to pass to the called workflow. Let's call this our "InvokerActivity" for now. Then, in InvokeWorkflowActivity, map the TargetWorkflow properties to the dependent properties of the InvokerActivity as Panos suggests above. NOTE. For the ellipsis to display objects, MUST be specific types. If your object is an interface, you won't be able to map it. The workflow doesn't know how to instantiate the interface object.
  • Place InvokerActivity inside ReplicatorActivity using constructor.
  • The ReplicatorActivity function provides a handler for the ChildInitialized event. Create your handler for this event and you will get the ReplicatorChildEventArgs in it. In it, you can receive Activities through args events as such:

        InvokerActivity activity = (e.Activity as InvokerActivity);
        if (activity != null)
        {
            activity.MyParam = e.InstanceData as MyParamType;
        }
    
          



Now when you run it, ReplicatorActivity will call this method once for each item in the collection and walk through the parameters for each of the InvokerActivities it will appear.

e.InstanceData will be the next object in the collection that the Replicator will execute.

+1


source


You can declare two properties in the target workflow like this:

    public static readonly DependencyProperty MyIntProperty =
        DependencyProperty.Register("MyInt", typeof(int), typeof(Workflow3));
    public static readonly DependencyProperty MyStringProperty =
        DependencyProperty.Register("MyString", typeof(string), typeof(Workflow3));

    public int MyInt
    {
        get { return (int)GetValue(MyIntProperty); }
        set { SetValue(MyIntProperty, value); }
    }

    public string MyString
    {
        get { return (string)GetValue(MyStringProperty); }
        set { SetValue(MyStringProperty, value); }
    }

      

After that, if you check the Properties tab in InvokeWorkflowActivity

, you will see two properties in the category Parameters

.

http://i35.tinypic.com/2czssuf.png

You can either provide constant values ​​or bind them to any hosting property.

0


source







All Articles