未能成功调用 Azure IoT Edge 模块直接方法

Didn't manage to call Azure IoT Edge module direct method

我是 运行 物联网边缘模块并且已经注册了方法回调。

SetMethodHandlerAsync 和 SetMethodDefaultHandlerAsync

但两者都没有被调用...

正在从 Azure 门户发起直接消息调用

Failed to invoke device method: {"message":"Device {\"Message\":\"{\"errorCode\":404103,\"trackingId\":\"126e3eef616c409385e73128aef94b21-G:17-TimeStamp:08/23/2018 12:29:35\",\"message\":\"Timed out waiting for device to connect.\",\"info\":{\"timeout\":\"00:00:10\"},\"timestampUtc\":\"2018-08-23T12:29:35.2214374Z\"}\",\"ExceptionMessage\":\"\"} not registered"}

来自VsCode

Failed to invoke Direct Method: Not found

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

从 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);
                    });
    }

当我使用以下命令停止模块时出现 "Timed out waiting for device to connect" 错误:

Stop-Service iotedge -NoWait

门户错误:

但是在你的错误信息中,有"not registered"信息。您的模块似乎没有连接到边缘设备或从未 运行 成功。

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

docker logs TestDirectMethodModule -f

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

iotedge list

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