Running pipeline blocks synchronously or asynchronously

Current version: 10.1

Starting with Sitecore XC 10.0, all pipeline blocks derive from either the AsyncPipelineBlock class or the SyncPipelineBlock class, depending on whether a method must run asynchronously or synchronously.

Using the AsyncPipelineBlock and SyncPipelineBlock classes

To execute a pipeline block synchronously, use the SyncPipelineBlock class. The method name used to run a block synchronously is Run(). For example:

RequestResponse
public class SampleSyncBlock : SyncPipelineBlock<string, string, CommercePipelineExecutionContext>
{   
    public override string Run(string arg, CommercePipelineExecutionContext context)
    {
        ////
        return arg;
    }
}

To run a pipeline block asynchronously, use the AsyncPipelineBlock. The method name used to run a block asynchronously is RunAsync(). For example:

RequestResponse
public class SampleAsyncBlock : AsyncPipelineBlock<string, string, CommercePipelineExecutionContext>
{   
    public override Task<string> RunAsync(string arg, CommercePipelineExecutionContext context)
    {
        ////
        return Task.FromResult(arg);
    }
}

The IPipeline RunAsync() method

Starting with Sitecore XC 10.0, the IPipeline interface method is changed from Run to RunAsync() because the method is an asynchronous operation.

Each pipeline can use a combination of synchronous and asynchronous blocks.

Conditional pipeline blocks

Starting with Sitecore 10.0, all conditional pipeline blocks must derive from either one of the following synchronous or asynchronous block classes:

  • ConditionalPipelineBLock

  • AsyncConditionalPipelineBLock

To run a conditional pipeline block synchronously, you derive it from the ConditionalPipelineBLock class. For example:

RequestResponse
public class SampleSyncConditionalPipelineBlock : ConditionalPipelineBlock<string, string, CommercePipelineExecutionContext>
{   
    public override string Run(string arg, CommercePipelineExecutionContext context)
    {
        return arg;
    }
    public override string ContinueTask(string arg, CommercePipelineExecutionContext context)
{
    return arg; 
}  

To run a conditional block asynchronously, you derive it from the AsyncConditionalPipelineBLock class. For example:

RequestResponse
public class SampleAsyncConditionalPipelineBlock : AsyncConditionalPipelineBlock<string, string, CommercePipelineExecutionContext>
{   
    public override Task<string> RunAsync(string arg, CommercePipelineExecutionContext context)
    {
      return Task.FromResult(arg);
    }
    public override Task><string> ContinueTask(string arg, CommercePipelineExecutionContext context)
    {
      return Task.FromResult(arg);
    }
}

Policy trigger conditional blocks

Starting with Sitecore 10.0, all policy trigger conditional blocks must derive from one of the following synchronous or asynchronous pipeline block classes:

  • SyncPolicyTriggerConditionalPipelineBLock

  • AsyncPolicyTriggerConditionalPipelineBLock

Note

A policy trigger block sets a header in the context that stops the block from running. For example, to skip cart calculation in a block, set ShouldNotRunPolicyTrigger to "DoNotCalculateCart".

To run a policy trigger conditional block synchronously, you derive it from the SyncPolicyTriggerConditionalPipelineBLock class. For example:

RequestResponse
public class SamplePolicyTriggerConditionalPipelineBlock : PolicyTriggerConditionalPipelineBlock<string, string>
{   
    public override string Run(string arg, CommercePipelineExecutionContext context)
    {
        return arg;
    }
    public override string ShouldNotRunPolicyTrigger => "DoNotRunThis";
}

To run a policy trigger conditional block asynchronously, you derive it from the AsyncPolicyTriggerConditionalPipelineBLock class. For example:

RequestResponse
public class SampleAsyncPolicyTriggerConditionalPipelineBlock : AsyncPolicyTriggerConditionalPipelineBlock<string, string>
{   
    public override Task<string> RunAsync(string arg, CommercePipelineExecutionContext context)
    {
        return Task.FromResult(arg);
    }
    public override string ShouldNotRunPolicyTrigger => "DoNotRunThis";
}

Do you have some feedback for us?

If you have suggestions for improving this article,