Moryx.AspNetCore.Mqtt
10.10.1
dotnet add package Moryx.AspNetCore.Mqtt --version 10.10.1
NuGet\Install-Package Moryx.AspNetCore.Mqtt -Version 10.10.1
<PackageReference Include="Moryx.AspNetCore.Mqtt" Version="10.10.1" />
<PackageVersion Include="Moryx.AspNetCore.Mqtt" Version="10.10.1" />
<PackageReference Include="Moryx.AspNetCore.Mqtt" />
paket add Moryx.AspNetCore.Mqtt --version 10.10.1
#r "nuget: Moryx.AspNetCore.Mqtt, 10.10.1"
#:package Moryx.AspNetCore.Mqtt@10.10.1
#addin nuget:?package=Moryx.AspNetCore.Mqtt&version=10.10.1
#tool nuget:?package=Moryx.AspNetCore.Mqtt&version=10.10.1
The goal:
The goal is to allow any MORYX developer to create their own Mqtt To MORYX facade, that way you can use Mqtt to communicate with any MORYX facade like:
ProductManagement,ResourceManagement etc.
To achieve that, we took inspiration from 3 repositories:
Checking those repositories will give you an idea of what the ManagedMqtt is trying to achieve.
APIs and classes:
IManagedMqttClient A managed
IMqttClientIMqttService : Preferred base class for all services based on Mqtt . IMqttEndpoint : Mqtt Endpoint interface. Every endpoint will implement this interface.Usage:
To use the
IManagedMqttClientyou need to configure it in theProgram.cslike so:builder.Services.AddMqttClient();Optionally you can also configure the Mqtt client connection details like so:
builder.Services.AddMqttClient((provider, options) => { options.Connection = new MqttConnectionConfig { Port = 1883, Id = "EndpointDemo1", Host = "localhost", QoS = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, RootTopic = "mqtt/moryx" }; // optional: add your custom converter/serializer options.JsonSerializerOptions.Converters.Add(new MyConverter()) })You can also configure the Mqtt client using a configuration file just like any
MORYXmodule. The configuration file can be found inside the default MORYX Config folder. The content is as follows:{ "Id": "my-id", "Host": "localhost", "Port": 1883, "Username": null, "Password": null, "Tls": false, "QoS": "ExactlyOnce", // check the MqttQualityOfServiceLevel enum for more info "ReconnectDelayMs": 30000, "ReconnectWithClientSession": false, "RootTopic": "" }Serialization :
The serialization of payload is done via
MqttMessageSerializationhelper class, serialization and deserialization methods requiresJsonSerializerOptionsthat is configured during setup of the Mqtt client.JsonSerializationOptions :
// Program.cs builder.Services.AddMqttClient(builder: (provider, options) => { options.JsonSerializerOptions.Converters.Add(new MyCustomConverter()) // here you add your custom converter options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // change how references are handled //etc.. }) //.. other codeIf you want to implement your custom service based on the
IManagedMqttClientthe recommended way is the following:- In your
Program.csadd the following:
builder.Services.AddMqttClient(); // this adds the IManagedMqttClient to the service collection- Create a new C# class and inherit from
IMqttService. Note: Dependency injection is supported. Now your service has access to theIManagedMqttClient.
To add your new service to the service collection do the following:
builder.Services.AddMqttClient(); .AddMqttService<BlablaService>(); // you custom serviceNote : the connection to the broker (Connect, re-connect on connection lost etc...) is automatically handled by the
IManagedMqttClient.- In your
Endpoints :
If you need something with topic template and constraint support, you can use the
IMqttEndpoint. Here is an example:- In your
program.csadd the following:
builder.Services.AddMqttClient() // must be present .AddMqttEndpoints(); // <-- this adds supports for the endpoints- Create a new C# class that implements the
IMqttEndpointExample.
There is a minimal version of the Mqtt endpoint, that you can use directly in the
Program.cs:builder.Services.AddMqttClient() .AddMqttEndpoints(route => { route.MapGet("my-topic/{id:int}/method/{methodName}", context => { var messageBuilder = new MqttApplicationMessageBuilder(); messageBuilder.WithTopic("another-topic"); messageBuilder.WithPayload(MqttMessageSerialization.GetJsonPayload("Hello World!!!!")); return messageBuilder.Build(); }); // optional : this behave like an eventStream route.MapPost(stream => { var messageBuilder = new MqttApplicationMessageBuilder(); messageBuilder.WithTopic("another-topic"); var timer = new Timer(async arg => { var messageBuilder = new MqttApplicationMessageBuilder(); messageBuilder.WithPayload(MqttMessageSerialization.GetJsonPayload("Hello World!!!!")); var message = messageBuilder.Build(); await stream.WriteAsync(message); }, null, 0, 10 * 1000); }); });This can be useful for quick prototyping and testing.
- In your
Available features in endpoints :
Endpoints topics support constraint just like in ASP.net controller route.
Route constraint :
Find the list of available constraints Here
Message Body and Parameters inside a Topic :
When the endpoint receives a message from the broker, it might come with a Body(payload) and a parameter inside the topic. To access the payload inside your endpoint you can use one of the following methods:
FromBody<T>(string propertyName)FromBody(string propertyName, Type type)//Return the entire body of the request cast to the given type T RequestBody<T>()//Get the request body as an Object RequestBodyObject()
More resources:
| Product | Versions 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. |
-
net10.0
- Moryx (>= 10.10.1)
- MQTTnet (>= 5.0.1.1416)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Moryx.AspNetCore.Mqtt:
| Package | Downloads |
|---|---|
|
Moryx.AbstractionLayer.Resources.Mqtt.Endpoints
MQTT Endpoints for MORYX Resources |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.10.1 | 100 | 8/24/2026 |
| 10.10.0 | 100 | 8/24/2026 |
| 10.9.1 | 107 | 8/8/2026 |
| 10.9.0 | 114 | 7/27/2026 |
| 10.8.0 | 121 | 7/10/2026 |
| 10.7.1 | 126 | 6/29/2026 |
| 10.7.0 | 128 | 6/29/2026 |
| 10.6.0 | 125 | 6/2/2026 |
| 10.5.0 | 116 | 5/18/2026 |
| 10.4.1 | 139 | 4/19/2026 |
| 10.4.0 | 121 | 4/5/2026 |
| 10.3.0 | 123 | 3/8/2026 |
| 10.2.0 | 144 | 2/25/2026 |