MessageValidation.MqttNet 1.0.0

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

MessageValidation.MqttNet

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

Installation

dotnet add package MessageValidation.MqttNet

Quick Start

Option A — Extension method on IMqttClient

Wire the pipeline directly onto an existing MQTTnet client:

using MessageValidation.MqttNet;
using MQTTnet;
using MQTTnet.Client;

var factory = new MqttFactory();
var client = factory.CreateMqttClient();

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

// One line — all incoming messages now go through the validation pipeline
client.UseMessageValidation(pipeline);

// Connect and subscribe as usual
await client.ConnectAsync(new MqttClientOptionsBuilder()
    .WithTcpServer("broker.example.com")
    .Build());

await client.SubscribeAsync("sensors/+/temperature");

Option B — DI registration

Let the DI container create and configure the client automatically:

using MessageValidation;
using MessageValidation.MqttNet;

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

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

// Register an IMqttClient with the validation pipeline pre-wired
builder.Services.AddMqttNetMessageValidation();

Then inject IMqttClient wherever you need it:

public class MqttWorker(IMqttClient mqttClient) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken ct)
    {
        await mqttClient.ConnectAsync(new MqttClientOptionsBuilder()
            .WithTcpServer("broker.example.com")
            .Build(), ct);

        await mqttClient.SubscribeAsync("sensors/+/temperature", cancellationToken: ct);

        // Messages are automatically deserialized, validated, and dispatched
        await Task.Delay(Timeout.Infinite, ct);
    }
}

Server-side validation

You can also hook the pipeline on an MQTTnet server to validate messages as they are published:

using MessageValidation.MqttNet;
using MQTTnet.Server;

var serverOptions = new MqttServerOptionsBuilder()
    .WithDefaultEndpoint()
    .Build();

var server = new MqttFactory().CreateMqttServer(serverOptions);

var pipeline = serviceProvider.GetRequiredService<MessageValidationPipeline>();
server.UseMessageValidation(pipeline);

await server.StartAsync();

MQTT Metadata

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

Key Type Description
mqtt.qos MqttQualityOfServiceLevel Quality of Service level
mqtt.retain bool Whether the message is retained
mqtt.clientId string Client ID (server-side only)
mqtt.contentType string MQTT 5.0 content type
mqtt.responseTopic string MQTT 5.0 response topic

Access them in your handler:

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

API Reference

IMqttClient.UseMessageValidation(MessageValidationPipeline pipeline)

Hooks the pipeline into the client's ApplicationMessageReceivedAsync event. Returns the same IMqttClient for chaining.

MqttServer.UseMessageValidation(MessageValidationPipeline pipeline)

Hooks the pipeline into the server's InterceptingPublishAsync event. Returns the same MqttServer for chaining.

AddMqttNetMessageValidation(Action<IMqttClient>? configureMqttClient = null)

Registers a singleton IMqttClient in the DI container with the validation pipeline pre-wired. Optional callback for additional client configuration.

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
1.0.0 86 4/1/2026
0.3.0 81 3/25/2026
0.2.0 88 3/9/2026
0.1.0-preview.1 41 3/4/2026