开发者

sharing data between workflow activities

开发者 https://www.devze.com 2023-04-04 02:44 出处:网络
I m with a list of custom activities(written in code behind, C#) where each is derived from NativeActivity, now I\'m adding all these activities to a sequence with the help of a foreach loop.

I m with a list of custom activities(written in code behind, C#) where each is derived from NativeActivity, now I'm adding all these activities to a sequence with the help of a foreach loop. now the question is, how I should proceed if I need to get some value from an activity and to pass it on to another activi开发者_JAVA百科ty.

Say, activity1 sets a string property value to "some file name"(lets an image file path) and based on that the activity next to it which so ever is added into the sequence with the help of for loop, takes it as input to flip that image.

Logic for getting the file is thre in Execute method of activity1 and the same to flip the image in Execute method of activity2.

thanks in advance


        var workflow = new Sequence();           
        Variable<Dictionary<string,object>> variable = new Variable<Dictionary<string,object>>
        {
            Name = "SharedData"
        };
        workflow.Variables.Add(variable);
        foreach (MyCustomActivity activity in mAddedActivities)
        {                
            workflow.Activities.Add(activity);                                      
        }

        WorkflowInvoker invoker = new WorkflowInvoker(workflow);
        invoker.Invoke();

This is what i Did for the actual implementation, there is no need of any inargument/outargument, The variable "Shared Data" is capable enough to hold the data across activities.

Now at each activity level in overridden code activity "Execute" method, you must use this excerpt of code to fetch input/fetch the value of this workflow variable, "SharedData".

        WorkflowDataContext dataContext = context.DataContext;
        PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties();
        foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection)
        {
            if (propertyDesc.Name == "SharedData")
            {
                myData = propertyDesc.GetValue(dataContext) as Dictionary<string, object>;
                if (myData == null) //this to check if its the initial(1st) activity.
                    myData = new Dictionary<string, object>();
                //I'm adding here an additional value into the workflow variable 
                //its having signature same as that of workflow variable
                //dictionary's key as what it is and value as an object
                //which user can cast to what actually one wants.
                myData.Add("islogonrequired", Boolean.TrueString);


                //here I'm fetching some value, as i entered it in my previous activity. 
                string filePath = myData["filepath"].ToString();
                propertyDesc.SetValue(dataContext, myData);
                break;
            }
        }

Hope this might help others.. Thanks everyone else out there for their help n support.


        var workflow = new Sequence();
        //Variable<string> v = new Variable<string>
        //{
        //    Name = "str" 
        //};

        //workflow.Variables.Add(v);

        Dictionary<string, object> abc = new Dictionary<string, object>();
        abc.Add("thedata", "myValue");

        foreach (MyCustomActivity activity in mAddedActivities)
        {
            if (activity.ActivityResult == null)
                activity.ActivityResult = new Dictionary<string, object>();
            activity.ActivityResult = abc;                
            workflow.Activities.Add(activity);

            //new Assign<string>
            //           {
            //               To = v,
            //               Value = activity.ActivityResult["thedata"].ToString()
            //           };                

        }
        WorkflowInvoker invoker = new WorkflowInvoker(workflow);
        invoker.Invoke();

this is what I did, and somehow it is working. I'm not sure as its right approach, suggest me something Will!!, here ActivityResult is property which is shared among the various added activities, via some sort of Interface member..

0

精彩评论

暂无评论...
验证码 换一张
取 消