Skip to content

2. Configuring the Pipeline

Alex McNair edited this page Jul 3, 2020 · 1 revision

You can override some of the default processing options by providing the registration method the options setup action.

var builder = new ContainerBuilder();

builder.RegisterCommandBus(
    new List<System.Reflection.Assembly>
    {
        // list the assemblies that contain your commands
    },
    opts =>
    {
        // set options
    });

The pipeline has 3 stages:

  • Authorization
  • Validation
  • Execution

By default, when a command is received by the command bus, it is authorized (if an auth provider exists), validated (if a validator exists) and then passed to its handler.

Processing Order

By default, authorization is performed before validation. This can be switched by setting AuthorizeBeforeValidate to false in the options setup action.

opts =>
{
    opts.AuthorizeBeforeValidate = false;
}

Authorization Failure

By default, if authorization fails, a System.UnauthorizedAccessException is thrown. This can be overridden by providing a callback in the options setup action. The callback receives the command metadata as its only argument.

NOTE: If a callback is provided that does not throw an exception, the processing pipeline WILL continue to the next stage.

opts =>
{
    opts.OnAuthorizationFailed = metadata =>
    {
        // custom auth failure logic
    };
}

Handler Exceptions

By default, if an exception is thrown by a command handler, the exception is unhandled and allowed to bubble up into your application, allowing you to handle the exception either globally, or inside each handler.

If you want to provide common exception handling inside ALL command handlers, you can provide a callback in the options setup action. The callback is passed the exception that was thrown as its only argument.

opts =>
{
    opts.OnHandlerException = ex =>
    {
        // custom exception handling
    };
}

Clone this wiki locally