MQTTnet.AspNetCore.AttributeRouting 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MQTTnet.AspNetCore.AttributeRouting --version 0.1.0
NuGet\Install-Package MQTTnet.AspNetCore.AttributeRouting -Version 0.1.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.AspNetCore.AttributeRouting" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MQTTnet.AspNetCore.AttributeRouting --version 0.1.0
#r "nuget: MQTTnet.AspNetCore.AttributeRouting, 0.1.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.
// Install MQTTnet.AspNetCore.AttributeRouting as a Cake Addin
#addin nuget:?package=MQTTnet.AspNetCore.AttributeRouting&version=0.1.0

// Install MQTTnet.AspNetCore.AttributeRouting as a Cake Tool
#tool nuget:?package=MQTTnet.AspNetCore.AttributeRouting&version=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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.3.16 8,014 10/18/2021
0.3.15 1,814 5/12/2021
0.3.14 638 2/12/2021
0.3.13 1,637 10/27/2020
0.3.0 1,549 8/14/2020
0.2.0 513 8/13/2020
0.1.0 546 8/12/2020

Beta release