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.

There is a newer version of this package available.
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" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MQTTnet.AspNetCore.AttributeRouting, 0.1.0"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.3.14 34 2/12/2021
0.3.13 675 10/27/2020
0.3.0 808 8/14/2020
0.2.0 131 8/13/2020
0.1.0 168 8/12/2020