MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Load config from config file

Open archerian opened this issue 8 years ago • 2 comments

Is there a way to load MQTT parameters like host, port, username etc. from a appsettings.json or similar config file? Something like WebHost.CreateDefaultBuilder calls serverOptions.Configure(context.Configuration.GetSection("Kestrel")) by default to load Kestrel configuration instead of having to manually get key value pairs and pass as arguments to the function calls?

archerian avatar Oct 12 '18 19:10 archerian

Hi, sorry but such a feature is currently not available.

Best regards Christian

chkr1011 avatar Oct 12 '18 21:10 chkr1011

I also need the possibility to read MQTT configuration from .NET configuration.

I checked a bit the code. I think it is currently not much missing for a basic support.

In common must be MqttClientOptions used instead if MqttClientOptionsBuilder. MqttClientOptions has already public properties which allow binding to .NET configuration according .NET Options Pattern

But I think only the property ChannelOptions can't be supported, because it is of type IMqttClientChannelOptions and could be of type MqttClientTcpOptions or MqttClientWebSocketOptions. But this property is mandatory for every usage. Both of the possible classes also fulfill the minimum requirements for .NET Options Pattern.

Maybe this can be solved by changing the code in MqttClientOptions from ...

public IMqttClientChannelOptions ChannelOptions { get; set; }

... into ...

public IMqttClientChannelOptions? ChannelOptions { get { TcpChannelOptions ?? WebSocketOptions;  } }
public MqttClientTcpOptions? TcpChannelOptions { get; set; }
public MqttClientWebSocketOptions? WebSocketOptions { get; set; }

It behaves like the Build() method, where also TCP is preferred. Possible alternative is a Enum-property which allows to decide between TPC or WebSocket

With that approach it should be possible to load the configuration from file or any other configuration source.

Sub-Topic: I think some/most of the helper methods in MqttClientOptionsBuilder can be also provided as extension method for MqttClientOptions.

I tried also other pubsub client libraries for other protocols and I must say that all I have seen provide such basic support. Please rethink your decision and provide this feature.

lichti81 avatar Aug 30 '23 15:08 lichti81