Search code examples
c#wpfmqtt

MqttNet Basic example


I have to create a very basic Mqtt broker-client demo I have therefore followed and downloaded this example.

It works perfectly but it is a console application with Net5.0.

I have to make it work on a wpf 4.5.2. solution. That should be possible according to this enter image description here

So taking as example the solution above

enter image description here

I have created a project of my own with the same referencies

enter image description here

Per each project I have also added the correct using statements as in the example.

So everything should be correct but when I paste the code for the Server I get those errors

MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
           .WithDefaultEndpoint()
           .WithDefaultEndpointPort(707)
           .WithConnectionValidator(OnNewConnection)
           .WithApplicationMessageInterceptor(OnNewMessage);


        IMqttServer mqttServer = new MqttFactory().CreateMqttServer();

enter image description here

and also for the client I get other errors

enter image description here

Where is the problem? Thanks

---ADD---

As requested here are the errors

(1) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'ConnectedHandler' and no accessible extension method 'ConnectedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  25  IntelliSense    Active

(2) Error   CS0246  The type or namespace name 'MqttClientConnectedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  48  IntelliSense    Active

(3) Error   CS0103  The name 'OnConnected' does not exist in the current context    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  83  IntelliSense    Active

(4) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'DisconnectedHandler' and no accessible extension method 'DisconnectedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  25  IntelliSense    Active

(5) Error   CS0246  The type or namespace name 'MqttClientDisconnectedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?) Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  51  IntelliSense    Active

(6) Error   CS0103  The name 'OnDisconnected' does not exist in the current context Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  89  IntelliSense    Active

(7) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'ConnectingFailedHandler' and no accessible extension method 'ConnectingFailedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  25  IntelliSense    Active

(8) Error   CS0246  The type or namespace name 'ConnectingFailedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?)   Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  55  IntelliSense    Active

(9) Error   CS0103  The name 'OnConnectingFailed' does not exist in the current context Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  87  IntelliSense    Active

(10) Error  CS1061  'IManagedMqttClient' does not contain a definition for 'ApplicationMessageReceivedHandler' and no accessible extension method 'ApplicationMessageReceivedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  47  25  IntelliSense    Active

(11) Error  CS0246  The type or namespace name 'MqttApplicationMessageReceivedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?) Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  47  65  IntelliSense    Active

(12) Error  CS1061  'IManagedMqttClient' does not contain a definition for 'PublishAsync' and no accessible extension method 'PublishAsync' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  56  29  IntelliSense    Active

Solution

  • MQTT is fast becoming one of the main protocols for IOT (internet of things) deployments.

    There are two different variants of MQTT and several versions.
    MQTT v3.1.0 –
    MQTT v3.1.1 – In Common Use
    MQTT v5 – Currently Limited use
    MQTT-SN – See notes later

    The example you follow use version 3.0.16. MQTT v3.1.1 is version in common use. There is very little difference between v3.10 and 3.1.1. so downgrade your MQTT to solve this problem.

    Install-Package MQTTnet -Version 3.0.16
    

    Client:

    Install-Package MQTTnet.Extensions.ManagedClient -Version 3.0.16
    

    ref: link1, link2, link3, link4