Pozitron.Extensions.MediatR 1.0.1

dotnet add package Pozitron.Extensions.MediatR --version 1.0.1                
NuGet\Install-Package Pozitron.Extensions.MediatR -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pozitron.Extensions.MediatR" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pozitron.Extensions.MediatR --version 1.0.1                
#r "nuget: Pozitron.Extensions.MediatR, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pozitron.Extensions.MediatR as a Cake Addin
#addin nuget:?package=Pozitron.Extensions.MediatR&version=1.0.1

// Install Pozitron.Extensions.MediatR as a Cake Tool
#tool nuget:?package=Pozitron.Extensions.MediatR&version=1.0.1                

A simple library that extends MediatR with various publishing strategies. I elaborated on the motivation and implementation details in this article.

Usage

Set the MediatorImplementationType to ExtendedMediator in the configuration.

builder.Services.AddMediatR(cfg =>
{
    cfg.MediatorImplementationType = typeof(ExtendedMediator);

    // All your desired configuration.
    cfg.RegisterServicesFromAssemblyContaining<Ping>();
});

Alternatively, use the AddExtendedMediatR extension. This extension will set the correct type for you.

builder.Services.AddExtendedMediatR(cfg =>
{
    // All your desired configuration.
    cfg.RegisterServicesFromAssemblyContaining<Program>();
});

The library defines a Publish extension that accepts a strategy as a parameter. You may choose a strategy on the fly.

public class Foo(IMediator mediator)
{
    public async Task Run(CancellationToken cancellationToken)
    {
        // The built-in behavior
        await mediator.Publish(new Ping(), cancellationToken);

        // Publish with specific strategy
        await mediator.Publish(new Ping(), PublishStrategy.WhenAll, cancellationToken);
    }
}

Publish Strategies

Currently, there is support for the following strategies.

public enum PublishStrategy
{
    /// <summary>
    /// The default publisher or the one set in MediatR configuration.
    /// </summary>
    Default = 0,

    /// <summary>
    /// Executes and awaits each notification handler one after another.
    /// Returns when all handlers complete or an exception has been thrown.
    /// In case of an exception, the rest of the handlers are not executed.
    /// </summary>
    Sequential = 1,

    /// <summary>
    /// Executes and awaits each notification handler one after another.
    /// Returns when all handlers complete. It continues on exception(s).
    /// In case of any exception(s), they will be flattened and captured in an AggregateException.
    /// The AggregateException will contain all exceptions thrown by all handlers, including OperationCanceled exceptions.
    /// </summary>
    SequentialAll = 2,

    /// <summary>
    /// Executes and awaits all notification handlers using Task.WhenAll. It does not create a separate thread explicitly.
    /// In case of any exception(s), they will be flattened and captured in an AggregateException.
    /// The AggregateException will contain all exceptions thrown by all handlers, including OperationCanceled exceptions.
    /// </summary>
    WhenAll = 3,

    /// <summary>
    /// Creates a single new thread using Task.Run(), and returns Task.Completed immediately.
    /// Creates a new scope using IServiceScopeFactory, executes and awaits all handlers sequentially.
    /// In case of an exception, it stops further execution. The exception is logged using ILogger<T> (if it's registered in DI).
    /// </summary>
    SequentialBackground = 11,

    /// <summary>
    /// Creates a single new thread using Task.Run(), and returns Task.Completed immediately.
    /// Creates a new scope using IServiceScopeFactory, executes and awaits all handlers sequentially.
    /// In case of exception(s), they are logged using ILogger<T> (if it's registered in DI).
    /// </summary>
    SequentialAllBackground = 12,

    /// <summary>
    /// Creates a single new thread using Task.Run(), and returns Task.Completed immediately.
    /// Creates a new scope using IServiceScopeFactory, executes and awaits all handlers using Task.WhenAll.
    /// In case of exception(s), they are logged using ILogger<T> (if it's registered in DI).
    /// </summary>
    WhenAllBackground = 13
}

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 131 10/18/2024
1.0.0 96 10/18/2024

Fixed XML documentation.