I am having ManagedMqttClient to establish connection to Solace.
public async Task Connect()
{
_mqttClient = new MqttFactory().CreateManagedMqttClient();
_mqttClientOptions = new MqttClientOptionsBuilder()
.WithClientId(_options.ClientId)
.WithTcpServer(_options.Host, _options.Port);
ManagedMqttClientOptions managedMqttClientOptions = new ManagedMqttClientOptionsBuilder()
.WithClientOptions(_mqttClientOptions)
.Build();
await _mqttClient.StartAsync(managedMqttClientOptions);
_mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(e =>
_logger.LogDebug("MQTT connection is made; Result code: {ConnectResult}", e.ConnectResult.ResultCode));
_mqttClient.ConnectingFailedHandler = new ConnectingFailedHandlerDelegate(e =>
_logger.LogError("MQTT connection is failed; Exception: {Exception}", e.Exception.Demystify()));
_mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(e =>
_logger.LogDebug("MQTT connection is end; Reason: {Reason}", e.Reason));
_mqttClient.UseApplicationMessageReceivedHandler(MessageReceivedHandler);
}
It works great for one instance of the service which is MQTT client.
However, when up another instance of the service in parallel I am facing with reconnection issue. It making connection and disconnection on both service every second.
Is there some way to use MQTTnet and scale my services without such problem. Thank you in advance!
MQTTnet packages:
<PackageReference Include="MQTTnet" Version="3.1.2" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.1.2" />