Do you want to invoke another workflow but you won’t know which one until runtime?


The InvokeWorkflowActivity that ships with WF requires you to set the type of the target workflow at design time.  If you won’t know which workflow to invoke until runtime you need to write a custom activity. To invoke a workflow need to use the IStartWorkflow service that you can get from the ActivityExecutionContext, like the following:


 


IStartWorkflow startWorkflow = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;


 


Guid instanceId = startWorkflow.StartWorkflow(this.TargetWorkflow, this.Parameters);


 


Since the parameters won’t be known until runtime you can’t use the WorkflowParameterBindingsCollection because you wouldn’t be able to change in at runtime.  Instead I have have a parameters property that is type Dictionary<string, object>.

CustomInvokeWorkflowActivity.exe

Comments (7)

  1. keshavbs says:

    Thanks for this sample.This is what I was exactly looking for

  2. En estos dias, he ordenado algunos enlaces y recursos sobre el Windows Workflow Foundation, el motor

  3. lodjikz says:

    Hi, thanks for this good sample.

    with this

    startWorkflow.StartWorkflow(this.TargetWorkflow, this.Parameters);

    we can start the workflow correctly.

    but how can we specify the method to be called to actually run the subworkflow, as in the invokeWorkflowActivity?

    Thanks

  4. SilverEarth says:

    Hi,

    Being new to wf,

    How would one go about setting up the basic InvokeWorkflowActivity from scratch using the designer?

    Can someone point me to a sample of how to do this please!

    Many thanks

  5. JB30 says:

    Help……

    I am using the ManualWorkflowSchedulerService to run my workflows

    How would the code changes?

    I tried to get the scheduler below but it returns null.

    scheduler = executionContext.GetService<ManualWorkflowSchedulerService>()

  6. Zhou Yu says:

    Hi Tom,

    Is it possible to also do this by using CallExternalMethod to invoke a local service method such as below?

    public void InvokeChildJobAsync(Type workflowType, Guid instanceId)

    {

       ThreadPool.QueueUserWorkItem(delegate(Object state)

       {

    WorkflowInstance instance = WorkflowRuntime.CreateWorkflow(workflowType, null, instanceId);

    instance.Start();

    this.SubmitJob(instanceId);

       });

    }

    The reason I am trying to do it this way is because of the line this.SubmitJob(instanceId), where I need to raise an event to the invoked workflow in addition to starting it.  Please let me know if this is feasible or if you see any issues.

    Thanks,

    -Zhou