MQTTnet.Extensions.ManagedClient.Routing 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MQTTnet.Extensions.ManagedClient.Routing --version 1.3.0
                    
NuGet\Install-Package MQTTnet.Extensions.ManagedClient.Routing -Version 1.3.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="MQTTnet.Extensions.ManagedClient.Routing" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MQTTnet.Extensions.ManagedClient.Routing" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="MQTTnet.Extensions.ManagedClient.Routing" />
                    
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 MQTTnet.Extensions.ManagedClient.Routing --version 1.3.0
                    
#r "nuget: MQTTnet.Extensions.ManagedClient.Routing, 1.3.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 MQTTnet.Extensions.ManagedClient.Routing@1.3.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=MQTTnet.Extensions.ManagedClient.Routing&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=MQTTnet.Extensions.ManagedClient.Routing&version=1.3.0
                    
Install as a Cake Tool

NuGet .NET CI Security License: MIT

MQTTnet Extensions ManagedClient Routing

MQTTnet Extension ManagedClient Routing is a fork of https://github.com/IoTSharp/MQTTnet.Extensions.ManagedClient.Routing

This addon to MQTTnet provides the ability to define controllers and use attribute-based routing against message topics in a manner that is very similar to AspNet Core.

Overview

MQTTnet.Extensions.ManagedClient.Routing extends MQTTnet's ManagedMqttClient with controller based routing. Use it when you want to organize MQTT handlers using MVC style controllers and attribute routes.

Installation

dotnet add package MQTTnet.Extensions.ManagedClient.Routing

Quickstart

A runnable end-to-end example lives in Samples/MqttRoutingSample. It starts an in-process broker, routes two messages through a controller and exits:

dotnet run --project Samples/MqttRoutingSample

Usage

Register your MQTT controllers in the DI container and enable routing:

builder.Services.AddMqttControllers();

app.UseAttributeRouting(); // or ((ManagedMqttClient)managedClient).WithAttributeRouting(app.Services);

AddMqttControllers scans an assembly for controllers. By default the entry assembly is used; pass assemblies explicitly (e.g. from tests or libraries):

builder.Services.AddMqttControllers([typeof(TelemetryController).Assembly]);

Example controller

public class TelemetryController : MqttBaseController
{
    [MqttRoute("telemetry/{deviceId:int}")]
    public async Task OnTemperature(int deviceId, [FromPayload] Reading reading)
    {
        Console.WriteLine($"Device {deviceId}: {reading.Celsius} °C");
        await Ok();
    }
}

Routes

A route template is a /-separated topic pattern. Literals match case-insensitively, {name} captures a segment, {name:type} validates and converts it, {name?} / {name:type?} are optional, {*rest} catches all remaining segments. Supported constraints: bool, datetime, decimal, double, float, guid, int, long (each optionally nullable with ?).

[controller] and [action] tokens are replaced with the controller (minus Controller suffix) and action names; escape them with double brackets ([[controller]]). A parameterless [MqttRoute] on an action falls back to the action name; on a controller it adds no route prefix.

Action signatures

Actions support void, Task, Task<T>, ValueTask and ValueTask<T> return types. Returned values are awaited and then discarded; throw to mark processing as failed. Parameters are bound from route values, from the JSON payload via [FromPayload], or from DI context:

  • CancellationToken tracks routing shutdown and, for ManagedMqttClient, the current connection. MaxActionExecutionTime adds an execution deadline. This token is cancellable even without an action deadline.
  • The current message is available via MqttBaseController.Message / MqttContext, or a [MqttControllerContext] property on custom controllers.
[MqttRoute("telemetry/{deviceId}")]
public async Task<int> OnTelemetry(int deviceId, [FromPayload] Reading reading, CancellationToken ct)
{
    await _store.SaveAsync(deviceId, reading, ct);
    return deviceId;
}

Interceptors

To execute code before and after each controller action, implement IRouteInvocationInterceptor and hook it up when adding the controllers:

builder.Services.AddMqttControllers(opt =>
    opt.WithRouteInvocationInterceptor<MyInterceptor>());

RouteExecuting is called before the handler runs and RouteExecuted afterwards (with the correlation object and any exception). See the architecture overview for details.

Failure handling

Each message is attempted once: handler, binding and interceptor failures mark the message as failed (ProcessingFailed) and are logged, but never fault the MQTT dispatch loop. There is no built-in retry or dead-letter queue — count failures in an IRouteInvocationInterceptor and forward poison messages to a dead-letter topic from there if needed.

Bound the blast radius of slow actions with opt.WithMaxActionExecutionTime(...) and, for independent messages only, raise opt.WithMaxConcurrentDispatches(...) (default 1, ordered).

The deadline also covers synchronous actions and synchronous work before an async action returns its task. On expiry the message is marked failed and the action token is canceled. Cancellation is cooperative: routing retains the DI scope and concurrency slot until the action actually terminates. An action that ignores cancellation can therefore hold a slot indefinitely.

In parallel mode routing disables MQTTnet's automatic acknowledgement and acknowledges successful messages after the action and RouteExecuted finish, in receive order within the original connection. Old deliveries cannot acknowledge packets on a replacement connection after reconnect. Failed QoS 1/2 deliveries remain unacknowledged; broker redelivery depends on the MQTT session. If automatic acknowledgement was already disabled before routing, the caller retains responsibility for acknowledging the message. Other receive handlers must not acknowledge a routed message prematurely or modify its processing result after handing it to background routing.

Configuration

MqttRoutingOptions (registered as singleton by AddMqttControllers):

  • SerializerOptions: JSON options for [FromPayload] deserialization (defaults to web defaults with full Unicode support; override via opt.WithJsonSerializerOptions(...)).
  • FromAssemblies: assemblies scanned for controllers (set via the AddMqttControllers overloads).
  • RouteInvocationInterceptor: interceptor type (set via WithRouteInvocationInterceptor<T>()).
  • MaxActionExecutionTime: optional positive action deadline (default null), up to the .NET timer maximum of 4,294,967,294 milliseconds; set via WithMaxActionExecutionTime. Invalid values throw InvalidOperationException during AddMqttControllers.
  • MaxConcurrentDispatches: maximum active dispatches, including timed-out actions still running (default 1, minimum 1); set via WithMaxConcurrentDispatches. Invalid values throw InvalidOperationException during AddMqttControllers.

Managed client reconnects grow exponentially with equal jitter (50–100% of the capped delay), with a one-second floor. MaxReconnectDelay defaults to two minutes and must be between one second and the .NET timer maximum of 4,294,967,294 milliseconds; values outside this range throw ArgumentException during StartAsync. Even very small base delays eventually reach this cap. Every new StartAsync resets the failure count. StopAsync called outside a callback waits for publishing, registered routed actions, and storage writes before releasing storage. Inside a managed-client callback or routed action it requests shutdown and returns without waiting for its own task; a later external call awaits full completion. StartAsync throws InvalidOperationException while shutdown is still in progress.

Resource ownership and shutdown

Routing awaits controller cleanup before disposing its asynchronous DI scope. Controllers and dependencies may implement IDisposable, IAsyncDisposable, or both; asynchronous cleanup takes precedence. Mark public cleanup methods with [NonAction] so controller discovery does not expose them as routes.

AddMqttControllers registers a hosted service that drains accepted dispatches during Host.StopAsync. For standalone clients, await client.StopAsync() outside a callback before disposing the service provider. Shutdown cancels action tokens and waits for actual termination, so handlers and cleanup operations must eventually complete. Disposing a container alone does not replace this shutdown sequence.

Versioning

Versions follow Semantic Versioning and are derived from v* git tags via MinVer. See CHANGELOG.md.

MIT License

This project is released under the MIT License.

Contributing

See CONTRIBUTING.md for guidelines on how to open issues, submit pull requests and run the test suite.

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.3.1 131 10/13/2026
1.3.0 462 10/12/2026
1.2.0 109 10/11/2026
1.1.0 941 7/22/2025