Shuttle.Core.Pipelines 21.0.1-beta

Prefix Reserved
This is a prerelease version of Shuttle.Core.Pipelines.
dotnet add package Shuttle.Core.Pipelines --version 21.0.1-beta
                    
NuGet\Install-Package Shuttle.Core.Pipelines -Version 21.0.1-beta
                    
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="Shuttle.Core.Pipelines" Version="21.0.1-beta" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shuttle.Core.Pipelines" Version="21.0.1-beta" />
                    
Directory.Packages.props
<PackageReference Include="Shuttle.Core.Pipelines" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shuttle.Core.Pipelines --version 21.0.1-beta
                    
#r "nuget: Shuttle.Core.Pipelines, 21.0.1-beta"
                    
#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.
#:package Shuttle.Core.Pipelines@21.0.1-beta
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shuttle.Core.Pipelines&version=21.0.1-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Shuttle.Core.Pipelines&version=21.0.1-beta&prerelease
                    
Install as a Cake Tool

Shuttle.Core.Pipelines

Observable event-based pipelines based broadly on pipes and filters.

Installation

dotnet add package Shuttle.Core.Pipelines

Configuration

In order to more easily make use of pipelines an implementation of the IPipelineFactory should be used. The following will register the PipelineFactory implementation:

services.AddPipelines(builder => {
    builder.AddAssembly(assembly);
});

This will register the IPipelineFactory as Scoped and, using the builder, add all IPipeline implementations as Transient and all IPipelineObserver implementations as Scoped.

Since pipelines are quite frequently extended by adding observers, the recommended pattern is to make use of an IHostedService implementation that accepts the PipelineOptions dependency:

public class CustomHostedService : IHostedService
{
    private readonly Type _pipelineType = typeof(InterestingPipeline);
    private readonly PipelineOptions _pipelineOptions; // Keep reference to unsubscribe if needed

    public CustomHostedService(IOptions<PipelineOptions> pipelineOptions)
    {
        _pipelineOptions = Guard.AgainstNull(Guard.AgainstNull(pipelineOptions).Value);

        _pipelineOptions.PipelineCreated += PipelineCreated;
    }

    private async Task PipelineCreated(PipelineEventArgs e, CancellationToken cancellationToken)
    {
        if (e.Pipeline.GetType() != _pipelineType)
        {
            return;
        }

        e.Pipeline.AddObserver(new SomeObserver());

        await Task.CompletedTask;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await Task.CompletedTask;
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        _pipelineOptions.PipelineCreated -= PipelineCreated;

        await Task.CompletedTask;
    }
}

Typically you would also have a way to register the above CustomHostedService with the IServiceCollection:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddCustomPipelineObserver(this IServiceCollection services)
    {
        services.AddHostedService<CustomHostedService>();

        return services;
    }
}

The above is a rather naive example but it should give you an idea of how to extend pipelines using the IPipelineFactory and IHostedService implementations.

Overview

A Pipeline is a variation of the pipes and filters pattern and consists of one or more stages that each contain one or more event types. When the pipeline is executed each event in each stage is raised in the order that they were registered. One or more observers should be registered to handle the relevant event(s).

Each Pipeline always has its own state that is simply a name/value pair with some convenience methods to get and set/replace values. The State class will use the full type name of the object as a key should none be specified:

var state = new State();
var list = new List<string> {"item-1"};

state.Add(list); // key = System.Collections.Generic.List`1[[System.String...]]
state.Add("my-key", "my-key-value");

Console.WriteLine(state.Get<List<string>>()[0]);
Console.Write(state.Get<string>("my-key"));

The Pipeline class has a AddStage method that will return a PipelineStage instance. The PipelineStage instance has a WithEvent method that will return a PipelineStage instance. This allows for a fluent interface to register events for a pipeline:

IPipelineObserver

The IPipelineObserver interface is used to define the observer that will handle the events:

public interface IPipelineObserver<TPipelineEvent> : IPipelineObserver where TPipelineEvent : class
{
    Task ExecuteAsync(IPipelineContext<TPipelineEvent> pipelineContext, CancellationToken cancellationToken = default);
}

The ExecuteAsync method is used for processing the event.

Pipeline Context

The IPipelineContext<T> provides access to the Pipeline instance, allowing observers to interact with the pipeline state or abort the pipeline.

public interface IPipelineContext<T> : IPipelineContext
{
}

public interface IPipelineContext
{
    IPipeline Pipeline { get; }
}

Example

Events should be simple classes (markers) as they do not carry data themselves; data is shared via the Pipeline State.

We will use the following events:

public class OnAddCharacterA;
public class OnAddCharacter;

In order for the pipeline to process the events we will have to define one or more observers to handle the events.

public class CharacterPipelineObserver : 
    IPipelineObserver<OnAddCharacterA>,
    IPipelineObserver<OnAddCharacter>
{
    public Task ExecuteAsync(IPipelineContext<OnAddCharacterA> pipelineContext, CancellationToken cancellationToken = default)
    {
        var state = pipelineContext.Pipeline.State;
        var value = state.Get<string>("value");

        value = string.Format("{0}-A", value);

        state.Replace("value", value);

        return Task.CompletedTask;
    }

    public Task ExecuteAsync(IPipelineContext<OnAddCharacter> pipelineContext, CancellationToken cancellationToken = default)
    {
        var state = pipelineContext.Pipeline.State;
        var value = state.Get<string>("value");
        var character = state.Get<char>("character");

        value = string.Format("{0}-{1}", value, character);

        state.Replace("value", value);

        return Task.CompletedTask;
    }
}

Next we will define the pipeline itself:

var pipeline = new Pipeline(pipelineDependencies); // Dependencies injected via DI in real app

pipeline.AddStage("process")
	.WithEvent<OnAddCharacterA>()
	.WithEvent<OnAddCharacter>();

pipeline.AddObserver(new CharacterPipelineObserver());

pipeline.State.Add("value", "start");
pipeline.State.Add("character", 'Z');

await pipeline.ExecuteAsync();

Console.WriteLine(pipeline.State.Get<string>("value")); // outputs start-A-Z

Advanced Features

Transaction Scope

Pipelines support TransactionScope which can be started at the beginning of a stage.

pipeline.AddStage("DatabaseOperation")
    .WithEvent<OnOperation>()
    .WithTransactionScope(); // Starts a TransactionScope for this stage

Event Ordering

You can inject events into an existing pipeline stage relative to other events:

// Add MyEvent before ExistingEvent
pipeline.GetStage("Process")
    .BeforeEvent<ExistingEvent>().Add<MyEvent>();

// Add MyEvent after ExistingEvent
pipeline.GetStage("Process")
    .AfterEvent<ExistingEvent>().Add<MyEvent>();
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on Shuttle.Core.Pipelines:

Package Downloads
Shuttle.Esb

Contains the core Shuttle.Esb assembly that should always be referenced when building Shuttle.Esb solutions.

Shuttle.Core.PipelineTransaction

Provides a pipeline observer to handle transaction scopes.

Shuttle.Recall

Event sourcing mechanism.

Shuttle.Core.PipelineTransactionScope

Provides a mechanism to create a transaction scope when a pipeline stage starts.

Shuttle.Core.Mediator.OpenTelemetry

OpenTelemetry instrumentation for Shuttle.Core.Mediator implementations.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
21.0.1-beta 131 2/7/2026
21.0.0-alpha 110 1/18/2026
20.0.0 4,437 2/2/2025
14.0.0 2,563 8/5/2024
13.0.0 4,570 4/30/2024
12.1.1 17,240 12/1/2022
12.0.0 28,655 9/4/2022
11.0.1 743 4/9/2022
11.0.0 30,070 3/21/2022
10.1.0 9,848 2/9/2021
10.0.7 20,589 11/27/2020
10.0.6 102,985 7/4/2018
10.0.5 1,571 4/12/2018
10.0.4 2,298 4/8/2018
10.0.3 1,315 4/7/2018
10.0.2 15,940 2/13/2018