views:

234

answers:

2

This seems to be more complicated than I first guessed. I have a workflow that needs to kick off another workflow in the code. How is this done? Do I have to get a reference to the current workflow instance's WorkflowRuntime first?

+7  A: 

I think that the InvokeWorkflowActivity might be what you are looking for. I have used it for launching workflows from within other workflows, and it works well.

Just be aware of the fact that the new workflows (just as all workflows) are executed asynchronously, so the "parent workflow" will continue executing directly after having launched the child workflow.

If you (for whatever reason) can't use the InvokeWorkflowActivity (for instance, if the new workflow is to be started from code within a service which is called from the parent workflow), you will in one way or another get hold of the workflow runtime instance.

The way I have approached this scenario is to declare an event in the service interface, and have the workflow host attach a listener to that event when the service is added to the runtime. Then you will have a code point that can be called from the service (through raising the event), that will also have access to the runtime; then you have all you need to launch the new workflow.

Fredrik Mörk
I was poking around with that...it looks like I create that object, assign the TargetWorkflow, assign the parameter bindings, and then...how in the world does the thing actually start executing?
Chris
The activity starts the workflow when it is being executed; you don't need to make it start by calling some method or so.
Fredrik Mörk
Thanks for the help. Any good examples for this method? Though it seems like the created workflow is hitting "initialized" at least, I'm not seeing it start, judging by a couple of Console.Writelines I have in a code activity that should be hit almost immediately.
Chris
+1  A: 

If you don't need to have an explicit activity, you can make use of the IStartWorkflow service which is available from the runtime.

Also, if you need synchronous execution, I'd take a look here to get started:

http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,33cfb35c-aca7-4a5e-8b35-ff983b0b83e4.aspx

MattK