views:

70

answers:

1

I am trying to write a custom CompositeActivity using WF3.5. Something like a WhileActivity.

The problem is when I want to execute the child activity again (it previously succeeded with a final call to ActivityExecutionContext.CloseActivity()) I get an InvalidOperationException, with this message: "Activity status must be 'Initialized' for executing".

From debugging the problem, it appears that the child activity's ExecutionStatus is Closed and its ExecutionResult is Uninitialized.

But the MSDN doc for ActivityExecutionContext.ExecuteActivity says that "If the status is Closed, the Activity is initialized and executed".

So why am I getting this exception and how can I avoid it?

Thanks, Julien

+1  A: 

I think I found the answer. The composite activity needs to create new ActivityExecutionContext for the child activity every time it wants to start the child activity.

Executing the child activity in a new ActivityExecutionContext:

ActivityExecutionContext context2 = executionContext.ExecutionContextManager.CreateExecutionContext(a);

// listen for the child's activity completion by implementing IActivityEventListener<ActivityExecutionStatusChangedEventArgs>
context2.Activity.RegisterForStatusChange(Activity.ClosedEvent, this);
context2.ExecuteActivity(context2.Activity);

Cleaning up after the activity is Closed:

// get the activity from the ActivityExecutionStatusChangedEventArgs, then...
activity.UnregisterForStatusChange(Activity.ClosedEvent, this);
ActivityExecutionContextManager executionContextManager = executionContext.ExecutionContextManager;

// close the child activity's execution context
executionContextManager.CompleteExecutionContext(executionContextManager.GetExecutionContext(activity));
Julien Couvreur