MediatR: A Noob Guide to IPipelineBehavior

M B A R KM B A R K
4 min read

One of the key features that sets MediatR apart is the use of pipeline behaviors, and in this blog post, we'll take a deep dive into IPipelineBehavior, a powerful tool for handling cross-cutting concerns.

Understanding IPipelineBehavior

IPipelineBehavior is an interface provided by MediatR that allows developers to define custom behaviors that will be executed as part of the request-response pipeline. These behaviors can intercept and modify both the request and the response, providing a flexible mechanism to inject cross-cutting concerns into your application.

Basic Structure of IPipelineBehavior

Before we delve into examples, let's take a look at the basic structure of the IPipelineBehavior interface:

public interface IPipelineBehavior<in TRequest, TResponse>
{
    Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next);
}
  • TRequest: The type of the request being processed.

  • TResponse: The type of the response returned by the handler.

  • Handle: The method where the behavior logic is implemented. It takes the request, a cancellation token, and a RequestHandlerDelegate<TResponse> parameter, which represents the next behavior or the final handler in the pipeline.

Example: Logging Behavior

Let's start with a simple example to illustrate how to use IPipelineBehavior for logging purposes. Suppose we want to log information about each request before it is handled. We can create a logging behavior like this:

public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;

    public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        _logger.LogInformation($"Handling {typeof(TRequest).Name}");
        var response = await next();
        _logger.LogInformation($"Handled {typeof(TRequest).Name}");

        return response;
    }
}

In this example, the LoggingBehavior logs information before and after handling the request. It uses the provided ILogger to output messages.

Integrating IPipelineBehavior into MediatR

To use our LoggingBehavior, we need to register it with MediatR. Assuming you have a .NET Core application, you can add the following code in your Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Other service registrations...

    services.AddMediatR(typeof(Startup)); // Register MediatR

    // Register LoggingBehavior
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
}

Now, every time a MediatR request is handled, our LoggingBehavior will log information about the request.

Real-world Use Cases

The power of IPipelineBehavior becomes evident in real-world scenarios. You can use it for various purposes, such as:

  • Validation: Implement behaviors to validate requests before processing.

  • Caching: Introduce caching behaviors to store and retrieve responses based on request parameters.

  • Exception Handling: Create behaviors to handle exceptions and provide consistent error handling across your application.

  • Authorization: Implement authorization behaviors to check user permissions before allowing the execution of certain requests.

Implementing an Exception Handling Behavior

Let's create an ExceptionHandlingBehavior that catches exceptions thrown during request processing and provides a centralized way to handle them:

public class ExceptionHandlingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<ExceptionHandlingBehavior<TRequest, TResponse>> _logger;

    public ExceptionHandlingBehavior(ILogger<ExceptionHandlingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        try
        {
            // Continue with the pipeline
            return await next();
        }
        catch (Exception ex)
        {
            _logger.LogError($"Exception occurred while handling {typeof(TRequest).Name}: {ex.Message}");

            // You can customize the exception handling logic here
            throw;
        }
    }
}

In this example, the ExceptionHandlingBehavior wraps the execution of the request handler in a try-catch block. If an exception occurs, it logs the error and rethrows the exception. You can customize the exception handling logic within the catch block based on your application's requirements.

Registering the Exception Handling Behavior

To integrate the exception handling behavior into your MediatR pipeline, register it in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Other service registrations...

    services.AddMediatR(typeof(Startup)); // Register MediatR

    // Register LoggingBehavior
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));

    // Register ExceptionHandlingBehavior
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ExceptionHandlingBehavior<,>));
}

By adding the ExceptionHandlingBehavior to the pipeline, you ensure that exceptions occurring during request processing are handled consistently across your application.

Customizing Exception Handling Logic

The ExceptionHandlingBehavior provides a central location to handle exceptions, allowing you to log errors, perform cleanup tasks, or even transform exceptions before they propagate further. Consider extending this behavior to suit your application's specific needs, such as notifying administrators or providing a friendlier user experience.

public class CustomExceptionHandlingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<CustomExceptionHandlingBehavior<TRequest, TResponse>> _logger;

    public CustomExceptionHandlingBehavior(ILogger<CustomExceptionHandlingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        try
        {
            return await next();
        }
        catch (ValidationException ex)
        {
            _logger.LogWarning($"Validation failed for {typeof(TRequest).Name}: {ex.Message}");
            // Handle validation-specific logic or transform the exception
            throw;
        }
        catch (Exception ex)
        {
            _logger.LogError($"Exception occurred while handling {typeof(TRequest).Name}: {ex.Message}");
            // Perform custom exception handling logic
            NotifyAdministrators(ex);
            // Transform the exception or rethrow based on application needs
            throw new CustomApplicationException("An unexpected error occurred.", ex);
        }
    }

    private void NotifyAdministrators(Exception ex)
    {
        // Implement notification logic here
    }
}

In this modified behavior, we've added a CustomExceptionHandlingBehavior that catches specific types of exceptions (ValidationException in this case) separately and provides custom handling logic.

1
Subscribe to my newsletter

Read articles from M B A R K directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

M B A R K
M B A R K

I’m a passionated .NET/C# developer, who likes to play around with C# and see how much I can do with it.