![]() |
VOOZH | about |
dotnet add package MQTTnet.AspNetCore.Routing --version 5.1.0
NuGet\Install-Package MQTTnet.AspNetCore.Routing -Version 5.1.0
<PackageReference Include="MQTTnet.AspNetCore.Routing" Version="5.1.0" />
<PackageVersion Include="MQTTnet.AspNetCore.Routing" Version="5.1.0" />Directory.Packages.props
<PackageReference Include="MQTTnet.AspNetCore.Routing" />Project file
paket add MQTTnet.AspNetCore.Routing --version 5.1.0
#r "nuget: MQTTnet.AspNetCore.Routing, 5.1.0"
#:package MQTTnet.AspNetCore.Routing@5.1.0
#addin nuget:?package=MQTTnet.AspNetCore.Routing&version=5.1.0Install as a Cake Addin
#tool nuget:?package=MQTTnet.AspNetCore.Routing&version=5.1.0Install as a Cake Tool
👁 NuGet Version
👁 License: MIT
👁 CI
👁 CI
👁 NuGet Downloads
MQTTnet AspNetCore Routing is a fork of https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting
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.
This library is a completely optional addon to MQTTnet (i.e. it is never required). You would WANT to use this if:
You can do everything that this addon does directly by using MQTTnet delegates yourself. However, as the amount of logic you write to validate or process incoming messages grows, the ability to organize your logic into controllers start to make much more sense. This library helps with organizing that code as well as bringing together your dependency injection framework to MQTTnet.
This library has not been tested against a very high-load environment yet. Ensure you do your own load testing prior to use in production. All performance improvement PRs are welcome.
This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet.AspNetCore.Routing/
Install this package and MQTTnet from nuget. For dotnet CLI:
dotnet add package MQTTnet.AspNetCore.Routing
Example configuration on ASP.NET Core 6 MVC Configuration
using MQTTnet.AspNetCore;
using MQTTnet.AspNetCore.Routing;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(o =>
{
o.ListenAnyIP(iotaboardMqttSettings.Port, l => l.UseMqtt());
o.ListenAnyIP(iotaboardHttpSettings.Port);
}
);
// Configure MQTTServer service
builder.Services
.AddHostedMqttServerWithServices(o =>
{
// other configurations
o.WithoutDefaultEndpoint();
})
.AddMqttConnectionHandler()
.AddConnections()
.AddMqttControllers( // <== NOTICE THIS PART
/*
By default, all controllers within the executing assembly are
discovered (just pass nothing here). To provide a list of assemblies
explicitly, pass an array of Assembly[] here.
*/)
/*
optionally, set System.Text.Json serialization default for use with
[FromPayload] in the controllers. We can specify a JsonSerializerOptions
or use JsonSerializerDefaults, useful for case-sensitivity or comment-handling
*/
.AddMqttDefaultJsonOptions(new JsonSerializerOptions(JsonSerializerDefaults.Web));
var app = builder.Build();
app.MapControllers();
app.UseMqttServer(server => {
// other MqttServer configurations, for example client connect intercepts
server.WithAttributeRouting(app.Services, allowUnmatchedRoutes: false);
});
app.Run();
Create your controllers by inheriting from MqttBaseController and adding actions to it like so:
[MqttController]
[MqttRoute("[controller]")] // Optional route prefix
public class MqttWeatherForecastController : MqttBaseController // Inherit from MqttBaseController for convenience functions
{
private readonly ILogger<MqttWeatherForecastController> _logger;
// Controllers have full support for dependency injection just like AspNetCore controllers
public MqttWeatherForecastController(ILogger<MqttWeatherForecastController> logger)
{
_logger = logger;
}
// Supports template routing with typed constraints just like AspNetCore
// Action routes compose together with the route prefix on the controller level
[MqttRoute("{zipCode:int}/temperature")]
public Task WeatherReport(int zipCode)
{
// We have access to the MqttContext
if (zipCode != 90210) { MqttContext.CloseConnection = true; }
// We have access to the raw message
var temperature = BitConverter.ToDouble(Message.Payload);
_logger.LogInformation($"It's {temperature} degrees in Hollywood");
// Example validation
if (temperature <= 0 || temperature >= 130)
{
return BadMessage();
}
return Ok();
}
// Supports binding JSON message payload to parameters with [FromPayload] attribute,
// Similar to ASP.NET Core [FromBody]
[MqttRoute("{deviceName}/telemetry")]
public async Task NewTelemetry(string deviceName, [FromPayload] Telemetry telemetry)
{
// here telemetry is JSON-deserialized from message payload to type Telemetry
bool success = await DoSomething(telemetry);
if (success) {
await Ok();
return;
}
else {
await BadMessage();
return;
}
}
}
See server example project here
See client example project here
Contributions are welcome. Please open an issue to discuss your idea prior to sending a PR.
See https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting/LICENSE.
This library is sponsored by Atlas Lift Tech. Atlas Lift Tech is a safe patient handling and mobility (SPHM) solution provider, helping hospitals improve patient outcomes and improve patient mobility.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on MQTTnet.AspNetCore.Routing:
| Package | Downloads |
|---|---|
|
IoTSharp
Open-source IoT Platform - Device management, data collection, processing and visualization. |
Showing the top 1 popular GitHub repositories that depend on MQTTnet.AspNetCore.Routing:
| Repository | Stars |
|---|---|
|
IoTSharp/IoTSharp
IoTSharp is an open-source IoT platform for data collection, processing, visualization, and device management.
|
* Added support for passing an array of assemblies to use in route discovery