我必须创建一个非常基本的 Mqtt 代理客户端演示,因此我遵循并下载了这个示例。
它工作得很好,但它是一个带有 Net5.0 的控制台应用程序。
我必须让它在 wpf 4.5.2 上工作。解决方案。按照这个应该是可以的

以上面的解决方案为例
我使用相同的参考创建了自己的项目
对于每个项目,我还添加了正确的 using 语句,如示例中所示。
所以一切都应该是正确的,但是当我粘贴服务器的代码时,我收到了这些错误
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.WithDefaultEndpointPort(707)
.WithConnectionValidator(OnNewConnection)
.WithApplicationMessageInterceptor(OnNewMessage);
IMqttServer mqttServer = new MqttFactory().CreateMqttServer();
Run Code Online (Sandbox Code Playgroud)
对于客户我也遇到其他错误
哪里有问题?谢谢
- -添加 - -
按照要求这里是错误
(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
Run Code Online (Sandbox Code Playgroud)
MQTT 正在迅速成为 IOT(物联网)部署的主要协议之一。
\nMQTT 有两种不同的变体和多个版本。\n
MQTT v3.1.0 \xe2\x80\x93\n
MQTT v3.1.1 \xe2\x80\x93 常用\n
MQTT v5 \xe2\x80\x93 目前限制使用\n
MQTT-SN \xe2\x80\ x93 参见稍后的注释
您遵循的示例使用版本3.0.16。MQTT v3.1.1 是常用版本。v3.10 和 3.1.1 之间差别很小。所以降级你的MQTT来解决这个问题。
\nInstall-Package MQTTnet -Version 3.0.16\nRun Code Online (Sandbox Code Playgroud)\n客户:
\nInstall-Package MQTTnet.Extensions.ManagedClient -Version 3.0.16\nRun Code Online (Sandbox Code Playgroud)\n\n