Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I try to encapsulate a Sequence of NativeActivity's in a NativeActivity, but I'm having trouble passing arguments to my "child" Activities. Tried a lot of different ways, just ended up with all possible exceptions of WF.
I wrote a small piece of code to illustrate the problematic, ouput should be
"Before TEST After", "TEST" being set in the main (and I will return a double as a result to this ParentActivity later).
Any help would be much appreciated!

Cheers,

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Statements;
using Microsoft.VisualBasic.Activities;
using System.Activities.Expressions;
using System.Reflection;

namespace ConsoleApplication1
{
    public class ChildActivity : NativeActivity<double>
    {
        public InArgument<String> MyChildInArgument { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            String test = MyChildInArgument.Get(context);
        }

    }

    public class ParentActivity : NativeActivity<double>
    {
        public InArgument<String> MyParentInArgument { get; set; }
        private DynamicActivity myDynamicActivity { get; set; }

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

        public ParentActivity()
        {
            myDynamicActivity = new DynamicActivity()
            {
                Properties =
        {
            new DynamicActivityProperty
            {
                Name = "MyProperty1",
                Type = typeof(InArgument<String>),
                Value = new InArgument<String>(),
            },
            new DynamicActivityProperty
            {
                Name = "MyProperty2",
                Type = typeof(String),
                Value = "DEFAULT",
            },
        },
                Implementation = () => new Sequence()
                {
                    Variables =
                    {

                    },
                    Activities =
            {
                new WriteLine()
                {
                     Text = "Before",
                },
                new ChildActivity()
                {
                     MyChildInArgument = (InArgument<String>)this.myDynamicActivity.Properties["MyProperty1"].Value,
                },
                new WriteLine()
                {
                     Text = "After",
                },
            },
                },
            };
        }

        protected override void Execute(NativeActivityContext context)
        {
            context.ScheduleActivity(myDynamicActivity);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            ParentActivity myTest = new ParentActivity()
            {
                MyParentInArgument = new InArgument<string>("TEST"),
            };
            WorkflowInvoker.Invoke(myTest);
        }
    }
}
Posted

1 solution

After all I was able to do it this way. Not exactly what I wanted but it should do.
Hope it will help someone:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Statements;
using Microsoft.VisualBasic.Activities;
using System.Activities.Expressions;
using System.Reflection;

namespace ConsoleApplication1
{
    public class ChildActivity : NativeActivity<double>
    {
        public InArgument<String> MyChildInArgument { get; set; }
        public OutArgument<String> MyChildOutArgument { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            String test = MyChildInArgument.Get(context);
        }

    }

    public class ParentActivity : Activity
    {
        public InArgument<String> MyParentInArgument { get; set; }
        public OutArgument<String> MyParentOutArgument { get; set; }

        protected override Func<Activity> Implementation
        {
            get
            {
                return () =>
                {
                    Sequence seq = new Sequence
                    {
                        Activities =
                        {
                            new WriteLine()
                            {
                                Text = "Before",
                            },
                            new ChildActivity()
                            {
                                MyChildInArgument = new InArgument<String>(new VisualBasicValue<String>("MyParentInArgument")),
                            },
                            new WriteLine()
                            {
                                Text = "After",
                            },
                            new Assign
                            {
                                To = new OutArgument<string>(new VisualBasicReference<string>("MyParentOutArgument")),
                                Value = new InArgument<string>(new VisualBasicValue<string>("MyParentInArgument"))
                            },
                        }
                    };
                    return seq;
                };
            }
            set
            {
                base.Implementation = value;
            }
        }


    }

    class Program
    {
        static void Main(string[] args)
        {
            ParentActivity myTest = new ParentActivity()
            {
                MyParentInArgument = new InArgument<string>("TEST"),
            };
            var output = WorkflowInvoker.Invoke(myTest);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900