I've created a custom native activity with collection of child activities. I am trying to cancel child activities if one of activities failed. Calling context.CancelChildren() does not invoke cancel in child activities.
Below is example:
public sealed class MyCompositeActivity : NativeActivity
{
public Collection<Activity> Activities { get; set; }
protected override void Execute(NativeActivityContext context)
{
ScheduleActivity(context);
}
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
metadata.SetChildrenCollection(Activities);
}
private void ScheduleActivity(NativeActivityContext context)
{
// Get next activity
....
context.ScheduleActivity(nextActivity, OnActivityCompleted, OnActivityFaulted);
}
private static void OnActivityFaulted(NativeActivityFaultContext context, Exception exception, ActivityInstance faultedInstance)
{
context.HandleFault();
context.CancelChildren();
}
}
Cancellation requires activities to be executing. So either it is an async activity waiting for a bookmark to be resumed or a composite activity waiting for the child activities to be completed. The most common place, though not the only one, where cancellation occurs is if there are multiple parallel branches and one branch throws an exception. The other banches can´t continue running as there was an unhandled exception so they are canceled.
From the code sample you provided it seems there is no parallel behavior and you are scheduling an activity after the previous one completes. As the single activity executing throws an error there are no others to cancel.