首页 > 解决方案 > 无法调用 Azure IoT Edge 模块直接方法

问题描述

我正在运行物联网边缘模块并已注册方法回调。

SetMethodHandlerAsync 和 SetMethodDefaultHandlerAsync

但两者都没有被称为...

从 Azure 门户发起直接消息呼叫

调用设备方法失败:{"message":"Device {\"Message\":\"{\\"errorCode\\":404103,\\"trackingId\\":\\"126e3eef616c409385e73128aef94b21-G:17-时间戳:08/23/2018 12:29:35\\",\\"message\\":\\"等待设备连接超时。\\",\\"info\\":{\\ "timeout\\":\\"00:00:10\\"},\\"timestampUtc\\":\\"2018-08-23T12:29:35.2214374Z\\"}\",\"ExceptionMessage \“:\“\“} 未注册”}

来自 VsCode

调用直接方法失败:未找到

我错过了设置任何必需的配置吗?
是否有命名约定或要指定的路径?

标签: azure-iot-edge

解决方案


从 VS 代码似乎调用设备而不是模块的方法,因为它不需要输入模块 ID。

从 Azure 门户,它对我有用。

在此处输入图像描述

注册方法并实现方法回调:

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

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

        await ioTHubModuleClient.SetMethodHandlerAsync("Write", WriteConsole, null);
        Console.WriteLine("IoT Hub module Set Method Handler:WriteConsole.");

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

    private static Task<MethodResponse> WriteConsole(MethodRequest methodRequest, object userContext)
    {
        return Task.Run( () => {
        Console.WriteLine($"Write direct method called!");
        return new MethodResponse(200);
                    });
    }

当我使用以下命令停止模块时,出现“等待设备连接超时”错误:

Stop-Service iotedge -NoWait

来自门户的错误:

在此处输入图像描述

但是在您的错误消息中,有“未注册”信息。您的模块似乎未连接到边缘设备或从未成功运行。

因此,您需要做的第一件事是检查模块日志并查看是否有任何错误。使用以下命令(替换您的模块名称而不是“TestDirectMethodModule”):

docker logs TestDirectMethodModule -f

使用以下命令检查所有模块的运行状态:

iotedge list

如果所有模块成功运行,您将看到以下结果:

在此处输入图像描述


推荐阅读