在不同模块之间发送直接方法调用的最佳实践? [英] Best practices for sending a direct method call between different modules?
问题描述
嗨.我正在尝试在同一个IoT Edge运行时上运行的两个不同模块之间调用具有直接方法请求的方法.
Hi. I'm trying to invoke a method with a direct method request between two different modules running on the same IoT Edge runtime.
我们叫模块ModuleA和ModuleB.
Let's call the modules ModuleA and ModuleB.
模块A具有称为代理"的直接方法.该请求将请求发送到称为"Sample1"的方法.在ModuleB中.
ModuleA have a direct method called "Proxy" that will send a request to a method called "Sample1" in ModuleB.
代理"方法在ModuleA的Init()函数中注册为:
The "Proxy" method is registered in the Init() function of ModuleA as:
await ioTHubModuleClient.SetMethodHandlerAsync("Proxy", Proxy, ioTHubModuleClient);
该方法本身看起来像:
static async Task<MethodResponse> Proxy(MethodRequest methodRequest, object moduleClient)
{
ModuleClient ioTHubModuleClient = (ModuleClient)moduleClient;
MethodRequest request = new MethodRequest("Sample1", Encoding.UTF8.GetBytes("{ \"Message\": \"Hello\" }"));
var resp = await ioTHubModuleClient.InvokeMethodAsync("my-iot-edge-device-id", "moduleB", request);
return resp;
}
因此,我正在尝试调用方法"Sample1".位于ModuleB中.
I'm thus trying to invoke the method "Sample1" located in ModuleB.
ModuleB中的Sample1方法定义为:
The Sample1 method in ModuleB is defined as:
private static Task<MethodResponse> Sample1(MethodRequest methodRequest, object userContext)
{
// Get data. We're not actually doing anything with this...
var data = Encoding.UTF8.GetString(methodRequest.Data);
// Print data sent to this method...
Console.WriteLine(data.ToString());
// Create and return method response
var methodResponse = new MethodResponse(Encoding.UTF8.GetBytes("{\"status\": \"ok\"}"), 200);
return Task.FromResult(methodResponse);
}
我从Azure门户调用名为代理"的方法.在ModuleA模块中,有效载荷为{}.
From Azure portal I invoke the method named "Proxy" in the ModuleA module with an empty payload of {} .
我的问题是ModuleA中的Proxy方法崩溃,但例外:
My issue is that the Proxy method in ModuleA crashes with the exception:
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in System.Private.CoreLib.dll
在线
推荐答案
您好,FredrikoPettersson,
Hello FredrikoPettersson,
我没有重现该错误.但是我遇到了以下错误.
I have not reproduced the error. But I encountered the following error.
Call Proxy Method.
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.<ExecuteAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.<PostAsync>d__17`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.ModuleClient.<InvokeMethodAsync>d__57.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SampleModuleA.Program.<Proxy>d__4.MoveNext() in /app/Program.cs:line 71
如果我没有在ModuleB中注册 Sample1 方法,那么将不会有任何错误信息.
If I don't register the Sample1 method in ModuleB, there will be no any error information.
await ioTHubModuleClient.SetMethodHandlerAsync("Sample1", Sample1, ioTHubModuleClient);
您可以参考这篇文章,它展示了如何使用Visual Studio Code通过Azure IoT Edge调试多个模块.
You can refer this article, it shows how to use Visual Studio Code to debug multiple modules with Azure IoT Edge.
最好的问候,
迈克尔
这篇关于在不同模块之间发送直接方法调用的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!