WF 4 Rehosted Designer - get foreach InArgument Value

谁说我不能喝 提交于 2019-12-11 11:19:25

问题


After reading this article:

http://blogs.msdn.com/b/tilovell/archive/2009/12/29/the-trouble-with-system-activities-foreach-and-parallelforeach.aspx

I have defined the ForEachFactory as follows:

public class ForEachFactory<T> : IActivityTemplateFactory
{
    public Activity Create(DependencyObject target)
    {
        return new ForEach<T>
        {
            DisplayName = "ForEachFromFactory",
            Body = new ActivityAction<T>
            {
                Argument = new DelegateInArgument<T>("item")
            }
        };
    }
}

All works well but is it possible to check how that DelegeateInArgument in my case named "item" changes its value ? So if i have defined an array in the variables section and initialized with {1, 2, 3} i need a way to check how the "item" takes value 1, 2 and then 3.

To be more accurate, i've added this pic, with a breakpoint on the WriteLine activity inside the foreach. When the execution will stop there, is there a way to find out what the value of item is ?

EDIT 1:

Possible solution in my case: After struggling a bit more i found one interesting thing:

Adding one of my custom activities in the Body of the ForEach, i am able to get the value of the item like this :

So, my activity derives from : CodeActivity

Inside the protected override String[] Execute(CodeActivityContext context) i am doing this job.To be honest, this solves the thing somehow, but it is doable only in my custom activities. If i would put a WriteLine there for example, i would not be able to retrieve that value.


回答1:


you can access the DelegeateInArgument of a ForEach activity by inspecting the ModelItem trees parent and checking for DelegeateInArgument's. If you need a specific code example to achieve this I may need a some time to code the example. As it has been a long time since I did this, see my question i asked over on msdn

So basically where your break point is, you can access the variable values as these are defined with n the scope of your activity as 'variables'. However the 'item' variable is actually only accessible from the parent loop activity. So you have to get the model item of the current executing activity and then traverse up the tree to find the parent containing the desired DelegateInArgument.

Can you flesh out exactly what you want to achieve? Is it that when your debugging the workflow in the re-hosted designer you want to display the variable values to the user as they change in the UI?

Edit - added tracking example

So as your wanting to display the variable values during execution of the workflow we need to use tracking to achieve this. In the example your using the author has already implemented some basic tracking. So to achieve the extended variable tracking you want you will need to alter the tracking profile.

Firstly amend the WorkflowDesignerHost.xaml.cs file alter the RunWorkflow method to define the SimulatorTrackingParticipant as below.

            SimulatorTrackingParticipant simTracker = new SimulatorTrackingParticipant()
            {
                TrackingProfile = new TrackingProfile()
                {
                    Name = "CustomTrackingProfile",
                    Queries = 
                    {
                        new CustomTrackingQuery() 
                        {
                            Name = all,
                            ActivityName = all
                        },
                        new WorkflowInstanceQuery()
                        {
                            **States = {all },**
                        },
                        new ActivityStateQuery()
                        {
                            // Subscribe for track records from all activities for all states
                            ActivityName = all,
                            States = { all },
                            **Arguments = {all},**
                            // Extract workflow variables and arguments as a part of the activity tracking record
                            // VariableName = "*" allows for extraction of all variables in the scope
                            // of the activity
                            Variables = 
                            {                                
                                { all }   
                            }
                        }   
                    }
                }
            };

This will now correctly capture all workflow instance states rather than just Started/Completed. You will also capture all Arguments on each activity that records tracking data rather than just the variables. This is important because the 'variable' were interested in is actually (as discussed earlier) a DelegateInArgument.

So once we have changed the tracking profile we also need to change the SimulatorTrackingParticipant.cs to extract the additional data we are now tracking.

If you change the OnTrackingRecordReceived method to include the following sections these will capture variable data and also Argument data during execution.

    protected void OnTrackingRecordReceived(TrackingRecord record, TimeSpan timeout)
    {
        System.Diagnostics.Debug.WriteLine(
            String.Format("Tracking Record Received: {0} with timeout: {1} seconds.", record, timeout.TotalSeconds)
        );

        if (TrackingRecordReceived != null)
        {
            ActivityStateRecord activityStateRecord = record as ActivityStateRecord;

            if (activityStateRecord != null)
            {
                IDictionary<string, object> variables = activityStateRecord.Variables;
                StringBuilder vars = new StringBuilder();

                if (variables.Count > 0)
                {
                    vars.AppendLine("\n\tVariables:");
                    foreach (KeyValuePair<string, object> variable in variables)
                    {
                        vars.AppendLine(String.Format(
                        "\t\tName: {0} Value: {1}", variable.Key, variable.Value));
                    }
                }

            }

            if (activityStateRecord != null)
            {
                IDictionary<string, object> arguments = activityStateRecord.Arguments;
                StringBuilder args = new StringBuilder();

                if (arguments.Count > 0)
                {
                    args.AppendLine("\n\tArgument:");
                    foreach (KeyValuePair<string, object> argument in arguments)
                    {
                        args.AppendLine(String.Format(
                        "\t\tName: {0} Value: {1}", argument.Key, argument.Value));
                    }
                }

                //bubble up the args to the UI for the user to see!
            }


            if((activityStateRecord != null) && (!activityStateRecord.Activity.TypeName.Contains("System.Activities.Expressions")))
            {
                if (ActivityIdToWorkflowElementMap.ContainsKey(activityStateRecord.Activity.Id))
                {
                    TrackingRecordReceived(this, new TrackingEventArgs(
                                                    record,
                                                    timeout,
                                                    ActivityIdToWorkflowElementMap[activityStateRecord.Activity.Id]
                                                    )

                        );
                }

            }
            else
            {
                TrackingRecordReceived(this, new TrackingEventArgs(record, timeout,null));
            }

        }

    }

Hope this helps!



来源:https://stackoverflow.com/questions/29960866/wf-4-rehosted-designer-get-foreach-inargument-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!