简体   繁体   中英

Sub Workflow in Windows Workflow Foundation and Persistence

I have a custom Activity that should call a sub workflow according to a name passed in an InArgument.

public class CallSubWorkflowActivtiy : NativeActivity {

 public InArgument<String> SelectedWorkflow { get; set; } protected override void Execute(NativeActivityContext context) { Activity subWorkflow = ActivityFactory.CreateActivity(SelectedWorkflow.Get(context)); context.ScheduleActivity(activity); } protected override void CacheMetadata(NativeActivityMetadata metadata) { } 

}

My prefered solution would be to invoke the child activity with context.ScheduleActivity . That doesn't work because I can't add metadata.AddImplementationChild(subWorkflow) in CacheMeta(context) because i haven't access to the InArguments at this point.

A solution could be to hack around this an access the InArguments in the CacheMetadata. But this isn't a solution that should be implemented.

Another solution I came up with was to use the WorkflowInstance or WorkflowInvoker to execute the subWorkflow Activities.

public class CallSubWorkflowActivtiy : NativeActivity { public InArgument SelectedWorkflow { get; set; }

 protected override void Execute(NativeActivityContext context) { Activity subWorkflow = ActivityFactory.CreateActivity(SelectedWorkflow.Get(context)); WorkflowApplication wfInstance = new WorkflowApplication(subWorkflow); } 

}

This solution would practically work. But I am not sure how the persistance is handled in this subworkflow an how the parent workflow continues when the child workflow finishes.

My questions are now:

  1. Is there any way to call SubWorkflow Activities with context.ScheduleActivity(...) if the activity wasn't added practically with metadata.AddImplementationChild(subWorkflow)
  2. If you use the WorkflowInstance class how is the persistance handled and how to continue the parent workflow if the child workflow finishes.

I solved my problem as follows:

 protected override void CacheMetadataInternal(System.Activities.NativeActivityMetadata metadata)
        {
            InArgument<String> workflowVersionArgument = SelectedWorkflow;
            if (workflowVersionArgument != null && workflowVersionArgument.Expression != null)
            {
                String selectedWorkflowString = workflowVersionArgument.Expression.ToString();
                if (selectedWorkflowString != null)
                {
                    WorkflowVersion wfVersion = WorkflowVersion.LoadFromXML(selectedWorkflowString);
                    if (wfVersion != null && wfVersion.WorkflowName != null)
                    {
                        VersionedActivity subWorkflow = ActivityFactory.Instance.CreateActivity(wfVersion.WorkflowName, wfVersion.Version);
                        if (subWorkflow != null && subWorkflow.ActivityProp != null)
                        {
                            subWorkflowInternal = subWorkflow.ActivityProp;
                            metadata.AddImplementationChild(subWorkflowInternal);
                        }
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM