Skip to content

Control Flow

Marty Mathis edited this page Nov 5, 2020 · 9 revisions

When a pipeline is executed, all components the pipeline is comprised of will be executed in the order specified. This will always occur unless one of two things happens.

Unhandled Exception

If an unhandled exception occurs in an underlying component during pipeline execution, this will terminate the pipeline without it finishing execution of any remaining components.

Component Specified Termination

There are certain scenarios where you might want a component to signal the pipeline is done executing for one reason or another. For example, a required api call might have failed to retrieve data required for the next component scheduled to execute. In this case, the component understands the pipeline really can't continue without the required data so it signals the framework to terminate execution. This is done by returning a null pipeline payload from the component like so:

    public class GetOrderComponent : AsyncPipelineComponentBase<PipelinePayload>
    {
        private IOrderClient _client;

        public GetOrderComponent(IOrderClient client)
        {
             _client = client;
        }

        public override async Task<PipelinePayload> ExecuteAsync(PipelinePayload payload, CancellationToken cancellationToken)
        {
            payload.Order = await _client.GetOrderAsync(payload.OrderId);
            
            if (payload.Order == null)
                //Terminate the pipeline because required data couldn't be retrieved.
                return null;  

            return payload;
        }
    }

Note: Returning a null Task is not the same as returning a null PipelinePayload. If the method is not marked async and null is returned, the AsyncPipeline will throw an exception. In this scenario, you would need to return Task.FromResult<PipelinePayload>(null) instead.