views:

81

answers:

2

I have a workflow, that at a certain point, needs to be triggered recursively.

I can't seem to figure out how to do this.

I tried the following code but context ends up being null??

private void codeTriggerChildren_ExecuteCode(object sender, EventArgs e)
{
    ActivityExecutionContext context = sender as ActivityExecutionContext;
    //context is null here?!

    IStartWorkflow aWorkflow = context.GetService(typeof(ApprovalFlow)) as IStartWorkflow;

    Dictionary<string, object> parameters = new Dictionary<string, object>();
    parameters.Add("Parm1", "foo");
    parameters.Add("Parm2", "bar");

    Guid guid = aWorkflow.StartWorkflow(typeof(ApprovalFlow), parameters);
}
A: 

Your sender is of type CodeActivity not ActivityExecutionContext. You need to create a custom activity and override the Execute method which will pass you a ActivityExecutionContext.

Maurice
How? I added an activity to the workflow diagram and I can't do anything with it
Chad
+2  A: 

Primarily the problem here is that the sender in this case is a CodeActivity not an ActivityExecutionContext. So this code fails at the first hurdle.

Here is an example of custom activity that can do what you are after:-

public class RecurseApproval : Activity
{
 protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
 {
  IStartWorkflow aWorkflow = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;

  Dictionary<string, object> parameters = new Dictionary<string, object>();
  parameters.Add("Param1", "Foo");
  parameters.Add("Param2", "bar");

  Guid guid = aWorkflow.StartWorkflow(typeof(ApprovalWorkflow), parameters);

  return ActivityExecutionStatus.Closed;

 }
}

Note that the GetService gets type of IStartWorkflow.

AnthonyWJones
And how do I create and execute that activity in code? The issue is the initial workflow needs to split itself into several children all running the same workflow.
Chad
You'd give this custom activity some dependency properties to feed the properties going into the workflow. You would then use a parrallel or a replicator activity to run this activity wiring up the properties accordingly. Finer control would require quite a tricky custom composite activity.
AnthonyWJones