MQTTnet.AspNetCore.AttributeRouting
0.3.14
This is a support library to integrate AttributeRouting into MQTTnet with AspNetCore.
Easily create Controllers and Actions to process incoming MQTT messages using attribute-based routing against the incoming message topic.
Install-Package MQTTnet.AspNetCore.AttributeRouting -Version 0.3.14
dotnet add package MQTTnet.AspNetCore.AttributeRouting --version 0.3.14
<PackageReference Include="MQTTnet.AspNetCore.AttributeRouting" Version="0.3.14" />
paket add MQTTnet.AspNetCore.AttributeRouting --version 0.3.14
#r "nuget: MQTTnet.AspNetCore.AttributeRouting, 0.3.14"
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.
When should I use this library?
This library is a completely optional addon to MQTTnet (i.e. it is never required). You would WANT to use this if:
- Your primary use case is validating/processing the MQTT messages on your server
- Your server is not primarily sending data to clients via MQTT
- You appreciate encapsulating your message processing logic in controllers in a way similar to AspNetCore and WebAPI
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.
Features
- Encapsulate your incoming message logic in controllers
- Use familiar paradigms from AspNetCore in your MQTT logic (Controllers and Actions)
- First-class support for dependency injection using existing ServiceProvider implementaiton in your AspNetCore project
- Support for sync and async/await Actions on your controllers
- Use together with any other MQTTnet options
Performance Note
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.
Supported frameworks
- .NET Standard 2.0+
- .NET Core 3.1+
Supported MQTT versions
- 5.0.0
- 3.1.1
- 3.1.0
Nuget
This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet.AspNetCore.AttributeRouting/
Usage
Install this package and MQTTnet from nuget.
Modify your Startup.cs
with the following options:
public void ConfigureServices(IServiceCollection services)
{
// ... All your other configuration ...
// Identify and build routes for the current assembly
services.AddMqttControllers();
services
.AddHostedMqttServerWithServices(s =>
{
// Optionally set server options here
s.WithoutDefaultEndpoint();
// Enable Attribute routing
s.WithAttributeRouting();
})
.AddMqttConnectionHandler()
.AddConnections();
}
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();
}
}
See a full example project here
Contributions
Contributions are welcome. Please open an issue to discuss your idea prior to sending a PR.
MIT License
See https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting/LICENSE.
About Atlas LiftTech
This library is sponsored by Atla LiftTech. Atlas Lift Tech is a safe patient handling and mobility (SPHM) solution provider, helping hospitals improve patient outcomes and improve patient mobility.
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.
When should I use this library?
This library is a completely optional addon to MQTTnet (i.e. it is never required). You would WANT to use this if:
- Your primary use case is validating/processing the MQTT messages on your server
- Your server is not primarily sending data to clients via MQTT
- You appreciate encapsulating your message processing logic in controllers in a way similar to AspNetCore and WebAPI
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.
Features
- Encapsulate your incoming message logic in controllers
- Use familiar paradigms from AspNetCore in your MQTT logic (Controllers and Actions)
- First-class support for dependency injection using existing ServiceProvider implementaiton in your AspNetCore project
- Support for sync and async/await Actions on your controllers
- Use together with any other MQTTnet options
Performance Note
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.
Supported frameworks
- .NET Standard 2.0+
- .NET Core 3.1+
Supported MQTT versions
- 5.0.0
- 3.1.1
- 3.1.0
Nuget
This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet.AspNetCore.AttributeRouting/
Usage
Install this package and MQTTnet from nuget.
Modify your Startup.cs
with the following options:
public void ConfigureServices(IServiceCollection services)
{
// ... All your other configuration ...
// Identify and build routes for the current assembly
services.AddMqttControllers();
services
.AddHostedMqttServerWithServices(s =>
{
// Optionally set server options here
s.WithoutDefaultEndpoint();
// Enable Attribute routing
s.WithAttributeRouting();
})
.AddMqttConnectionHandler()
.AddConnections();
}
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();
}
}
See a full example project here
Contributions
Contributions are welcome. Please open an issue to discuss your idea prior to sending a PR.
MIT License
See https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting/LICENSE.
About Atlas LiftTech
This library is sponsored by Atla LiftTech. Atlas Lift Tech is a safe patient handling and mobility (SPHM) solution provider, helping hospitals improve patient outcomes and improve patient mobility.
Release Notes
* Upgraded dependencies to v3.0.14
Dependencies
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Bcl.HashCode (>= 1.1.1)
- MQTTnet (>= 3.0.14)
- MQTTnet.AspNetCore (>= 3.0.14)
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http.Connections (>= 1.1.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Bcl.HashCode (>= 1.1.1)
- MQTTnet (>= 3.0.14)
- MQTTnet.AspNetCore (>= 3.0.14)
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.