MessageValidation.NatsNet 2.1.0

dotnet add package MessageValidation.NatsNet --version 2.1.0
                    
NuGet\Install-Package MessageValidation.NatsNet -Version 2.1.0
                    
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="MessageValidation.NatsNet" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MessageValidation.NatsNet" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MessageValidation.NatsNet" />
                    
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 MessageValidation.NatsNet --version 2.1.0
                    
#r "nuget: MessageValidation.NatsNet, 2.1.0"
                    
#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 MessageValidation.NatsNet@2.1.0
                    
#: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=MessageValidation.NatsNet&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MessageValidation.NatsNet&version=2.1.0
                    
Install as a Cake Tool

MessageValidation.NatsNet

NATS.Net transport adapter for the MessageValidation pipeline — automatically feed incoming NATS messages into the validation pipeline with a single line of code.

Installation

dotnet add package MessageValidation.NatsNet

Quick Start

Option A — Extension method on INatsConnection

Wire the pipeline directly onto an existing NATS connection:

using MessageValidation.NatsNet;
using NATS.Net;

await using var client = new NatsClient("nats://localhost:4222");
var connection = client.Connection;

var pipeline = serviceProvider.GetRequiredService<IMessageValidationPipeline>();

// One line — all incoming messages now go through the validation pipeline
await connection.SubscribeWithMessageValidationAsync(
    subject: "sensors.*.temperature",
    pipeline: pipeline,
    ct: stoppingToken);

Option B — DI registration

Let the DI container create and configure the connection automatically:

using MessageValidation;
using MessageValidation.NatsNet;

builder.Services.AddMessageValidation(options =>
{
    options.MapSource<TemperatureReading>("sensors.*.temperature");
    options.DefaultFailureBehavior = FailureBehavior.Log;
});

builder.Services.AddMessageDeserializer<JsonMessageDeserializer>();
builder.Services.AddMessageHandler<TemperatureReading, TemperatureHandler>();

// Register an INatsConnection ready to be used with the pipeline
builder.Services.AddNatsNetMessageValidation("nats://localhost:4222");

Then inject INatsConnection wherever you need it:

public class NatsWorker(
    INatsConnection connection,
    IMessageValidationPipeline pipeline) : BackgroundService
{
    protected override Task ExecuteAsync(CancellationToken ct) =>
        connection.SubscribeWithMessageValidationAsync(
            subject: "sensors.*.temperature",
            pipeline: pipeline,
            ct: ct);
}

Queue groups (load balancing)

Use a queue group to load-balance messages across multiple subscribers:

await connection.SubscribeWithMessageValidationAsync(
    subject: "orders.>",
    pipeline: pipeline,
    queueGroup: "order-workers",
    ct: stoppingToken);

NATS Metadata

When the adapter creates a MessageContext, it populates the Metadata dictionary with NATS-specific properties:

Key Type Description
nats.subject string The subject the message was delivered on
nats.replyTo string The reply-to subject, if any
nats.headers NatsHeaders Message headers, if any
nats.queueGroup string Queue group name, if subscribed with one

Access them in your handler:

public class TemperatureHandler : IMessageHandler<TemperatureReading>
{
    public Task HandleAsync(
        TemperatureReading message, MessageContext context, CancellationToken ct = default)
    {
        var subject = context.Metadata["nats.subject"];
        Console.WriteLine($"[{subject}] Sensor {message.SensorId}: {message.Value}°C");
        return Task.CompletedTask;
    }
}

API Reference

INatsConnection.SubscribeWithMessageValidationAsync(string subject, IMessageValidationPipeline pipeline, string? queueGroup = null, CancellationToken ct = default)

Subscribes to the specified subject and pushes every received message through the pipeline. Returns a Task that completes when the subscription loop is cancelled.

AddNatsNetMessageValidation(string? url = null, Action<NatsOpts>? configureOptions = null)

Registers a singleton INatsConnection in the DI container.

Requirements

License

MIT

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

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
2.1.0 88 7/16/2026