从 VS 代码似乎调用设备而不是模块的方法,因为它不需要输入模块 ID。
从 Azure 门户,它对我有用。
![在此处输入图像描述](https://i.stack.imgur.com/CzgWl.png)
注册方法并实现方法回调:
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
来自门户的错误:
![在此处输入图像描述](https://i.stack.imgur.com/IG30T.png)
但是在您的错误消息中,有“未注册”信息。您的模块似乎未连接到边缘设备或从未成功运行。
因此,您需要做的第一件事是检查模块日志并查看是否有任何错误。使用以下命令(替换您的模块名称而不是“TestDirectMethodModule”):
docker logs TestDirectMethodModule -f
使用以下命令检查所有模块的运行状态:
iotedge list
如果所有模块成功运行,您将看到以下结果:
![在此处输入图像描述](https://i.stack.imgur.com/EoJNC.png)