首页 > 解决方案 > 具有 1 个以上输入的 IoT Edge 模块失败。在一个模块中使用“input1”和“input2”测试不成功

问题描述

C# 中的 IoT Edge 模块可以有多少个消息输入?这些示例都显示“input1”,但除此之外没有其他内容。文档没有提到“input1”之外的任何内容。

我正在尝试在单个 IoT Edge C# 模块中使用多个输入,但不知道该怎么做。示例程序设置“input1”连接到名为 PipeMessage 的输入消息处理程序。当将该模块与默认路由一起使用时,一切正常。当尝试添加第二个输入消息处理程序并调用输入“input2”并添加一些路由时,什么都没有通过。我们如何在 C# IoT Edge 模块中设置多个输入?

program.cs Task Init() 使用称为 PipeMessage 的回调方法设置输入消息处理程序。我复制了这段代码并更改了第二个输入的名称,但不行。

 static async Task Init()
 {
        AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
        ITransportSettings[] settings = { amqpSetting };

        // Open a first connection to the Edge runtime
        ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
        await ioTHubModuleClient.OpenAsync();
        Console.WriteLine("IoT Hub module client initialized.");

        // Register callback to be called when a message is received by the module on input1
        await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);

        // Open a second connection to the Edge runtime
        ModuleClient ioTHubModuleClient2 = await ModuleClient.CreateFromEnvironmentAsync(settings);
        await ioTHubModuleClient2.OpenAsync();
        Console.WriteLine("IoT Hub module client initialized.");

        // Register callback to be called when a message is received by the module on input2
        await ioTHubModuleClient2.SetInputMessageHandlerAsync("input2", PipeMessage2, ioTHubModuleClient);
}

`

路线如下:

    "routes": {
        "CSharpFilter2ToIoTHub": "FROM /messages/modules/CSharpFilter2/outputs/* INTO $upstream",
        "sensor1ToCSharpFilter2": "FROM /messages/modules/tempSensor1/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/CSharpFilter2/inputs/input1\")",
        "sensor2ToCSharpFilter2": "FROM /messages/modules/tempSensor2/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/CSharpFilter2/inputs/input2\")"
      },

我究竟做错了什么?

更具体地说,我们应该如何处理 IoT Edge 模块中的多个输入?

标签: azure-iot-edge

解决方案


您不需要(或应该)创建第二个 ModuleClient。相反,只需多次调用 SetInputMessageHandlerAsync() :

ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
await ioTHubModuleClient.OpenAsync();   

await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage1, ioTHubModuleClient);
await ioTHubModuleClient.SetInputMessageHandlerAsync("input2", PipeMessage2, ioTHubModuleClient);

推荐阅读