在工作流活动之间共享数据

时间:2011-09-10 07:35:11

标签: c# .net .net-4.0 workflow-foundation workflow-foundation-4

我有一个自定义活动列表(用C#编写),其中每个都来自NativeActivity,现在我在foreach循环的帮助下将所有这些活动添加到序列中。 现在的问题是,如果我需要从活动中获取某些价值并将其传递给另一项活动,我应该如何继续。

假设,activity1将一个字符串属性值设置为“某个文件名”(允许一个图像文件路径),并根据它在它旁边的活动,借助于for循环将其添加到序列中,取得它作为翻转该图像的输入。

获取文件的逻辑是activity1的Execute方法,并且在activity2的Execute方法中翻转图像。

提前致谢

2 个答案:

答案 0 :(得分:2)

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

这就是我对实际实现所做的,不需要任何参与/争论,变量“共享数据”足以保存跨活动的数据。

现在,在重写代码活动“执行”方法的每个活动级别,您必须使用此代码摘录来获取输入/获取此工作流变量“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;
            }
        }

希望这可以帮助别人.. 谢谢其他所有人的支持。

答案 1 :(得分:0)

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

这就是我所做的,不知何故,它正在发挥作用。我不确定它是正确的方法,建议我一些意思!!,这里ActivityResult是通过某种接口成员在各种添加的活动之间共享的属性..