MQTTnet.AspNetCore.AttributeRouting
0.1.0
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.
See the version list below for details.
Install-Package MQTTnet.AspNetCore.AttributeRouting -Version 0.1.0
dotnet add package MQTTnet.AspNetCore.AttributeRouting --version 0.1.0
<PackageReference Include="MQTTnet.AspNetCore.AttributeRouting" Version="0.1.0" />
paket add MQTTnet.AspNetCore.AttributeRouting --version 0.1.0
#r "nuget: MQTTnet.AspNetCore.AttributeRouting, 0.1.0"
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.
Features
- Encapsulate your incoming message logic in controllers
- Use familiar paradigms from AspNetCore in your MQTT logic
- First-class support for dependency injection using existing ServiceProvider implementaiton in your AspNetCore project
- 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
{
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 async 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");
if (temperature <= 0 && temperature >= 130)
{
// Example validation
MqttContext.AcceptPublish = false;
}
}
}
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
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.
Features
- Encapsulate your incoming message logic in controllers
- Use familiar paradigms from AspNetCore in your MQTT logic
- First-class support for dependency injection using existing ServiceProvider implementaiton in your AspNetCore project
- 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
{
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 async 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");
if (temperature <= 0 && temperature >= 130)
{
// Example validation
MqttContext.AcceptPublish = false;
}
}
}
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
Release Notes
Beta release
Dependencies
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Bcl.HashCode (>= 1.1.0)
- MQTTnet (>= 3.0.12)
- MQTTnet.AspNetCore (>= 3.0.12)
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http.Connections (>= 1.1.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Bcl.HashCode (>= 1.1.0)
- MQTTnet (>= 3.0.12)
- MQTTnet.AspNetCore (>= 3.0.12)
Used By
GitHub repositories
This package is not used by any popular GitHub repositories.