mqtt – How to get MqttClientPublishResult from PublishAsync without error thrown?

I am testing my error handling when calling Mqttnet’s PublishAsync() to a bad topic, such as “####”, but I am getting unexpected behavior.

Expected:
I expected PublishAsync() to return an MqttClientPublishResult instance with ReasonCode having value MqttClientPublishReasonCode.TopicNameInvalid.

Actual:
Instead, I am getting exception:

MQTTnet.Exceptions.MqttProtocolViolationException: ‘The character ‘#’ is not allowed in topics.

This is rather unfortunate, because using specific MqttClientPublishReasonCode values from the MqttClientPublishResult would help my error handling. Is there a way to receive this result object back without the method throwing an error?

I’ve tried instantiating my client using MqttClientOptionsBuilder with its methods WithRequestProblemInformation() and WithRequestResponseInformation(), but the error still gets thrown. I know that a solution to this exists with PingAsync() by calling TryPingAsync(), but I cannot find a similar alternative for PublishAsync().

Read more here: Source link