Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible error in the implementation of handling events/functions added to ApplicationMessageReceivedAsync ? #2044

Open
stefan-nearbystore opened this issue Jul 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@stefan-nearbystore
Copy link

Hi all!

Due to that I don't know if these are bugs or features I make an issue as well out of this.
Added in Q&A as well!

I'm working on a "system controller"/console application that shall monitor and control a few control units (ESP32 based; but not active in these scenarios described here; this is just in the "test" environment in Windows 11) via MQTT.
Connecting to a cloud based (right now ...) broker/server (tried both HiveMQ and emqx variants).

In the application I have, as of right now, three client instances contained in separate own developed "client handlers" that add some extra functionality on top of the MQTTnet client.

I'm adding two event functions to all three of these clients for handling incoming MQTT messages/topics.

The code looks like this:
And _mqttClient is created via your framework as:
_mqttClient = factory.CreateMqttClient();

                if (_firstConnect)
                {
                    // What happens if the same message function is added more than ones ???
                    _mqttClient.ApplicationMessageReceivedAsync -= LogReceivedMessages;
                    _mqttClient.ApplicationMessageReceivedAsync += LogReceivedMessages;
                    if (receivedMessageEvent != null)
                    {
                        _mqttClient.ApplicationMessageReceivedAsync -= receivedMessageEvent;
                        _mqttClient.ApplicationMessageReceivedAsync += receivedMessageEvent;
                    }
                    // => The method/function is triggered more than ones !!!
                    // => First always remove the function and then add it you don't get this behavior!
                    _firstConnect = false;
                }

It's "in development" so I have both a check if there are "first connect" if doing the connect for the first time and both removing and adding the two functions (first LogReceivedMessages and second receivedMessageEvent here!) as of now (what is best I also do not know ..).

The log function looks like this:

    private async Task LogReceivedMessages(MqttApplicationMessageReceivedEventArgs e)
    {
        if (LogMessages)
        {
            Log($"---> Received message ...");
            // Log($"From ClientId: {e.ClientId}"); // TODO: Can you get what client that sent the message ??? 
            Log($"Topic received: {e.ApplicationMessage.Topic}");
            Log($"Payload/message received: {e.ApplicationMessage.ConvertPayloadToString()}");

            // TODO: How get whole message content by default ???
            // Log($"Event args: {e.ToString}");
            // Log($"Event args: {e.}");
        }
    }

and is for being able to log incoming messages if that is activated.

The second looks like this and is added as reference via the "receivedMessageEvent" function variable:

    private async Task ReceivedMQTTMessageEventStoreLocalCom(MqttApplicationMessageReceivedEventArgs args)
    {
        // Handle messages from local broker
        throw new NotImplementedException("ReceivedMQTTMessageEventStoreLocalCom");
    }

So this one throws an exception, important to notice!

Now to the questions and possible bugs:

  1. The exception do not show up in the the console application windows but can be seen in the Visual Studio 2022 window for the debug output. Why?

  2. If I change the order of how the events are added (so instead first adding receivedMessageEvent and second LogReceivedMessages) the second, LogReceivedMessages, are not triggered at all. So the first function are triggered which can be seen from the debug output. BUT then the second function, LogReceivedMessages, is skipped/not triggered?
    Are this as it should? Or are the try and catch in your implementation wrongly situated!? Unsure how this works in the MQTTnet implementation ...

@stefan-nearbystore stefan-nearbystore added the bug Something isn't working label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant