I'm using the Azure IoT Edge runtime running Linux containers on a Windows host OS. I have two modules, ModuleA and ModuleB. ModuleA have a registered direct method called "MethodA" and ModuleB have a registered direct method called "MethodB".
When I invoke MethodA I want the method to invoke MethodB located in another module (but running in the same IoT Edge runtime).
I'm using the Azure IoT SDK for c# and in the Init() function of ModuleA I have:
await ioTHubModuleClient.SetMethodHandlerAsync("MethodA", MethodA, ioTHubModuleClient);
And in the Init() function of ModuleB I have:
await ioTHubModuleClient.SetMethodHandlerAsync("MethodB", MethodB, null);
The MethodA (that acts like a proxy method):
static async Task<MethodResponse> MethodA(MethodRequest methodRequest, object moduleClient)
{
try
{
ModuleClient ioTHubModuleClient = (ModuleClient)moduleClient;
// Get deviced id of this device, exposed as a system variable by the iot edge runtime
var deviceId = System.Environment.GetEnvironmentVariable("IOTEDGE_DEVICEID");
// Create the request
MethodRequest request = new MethodRequest("MethodB", Encoding.UTF8.GetBytes("{ \"Message\": \"Hello\" }"));
// Execute request
var resp = await ioTHubModuleClient.InvokeMethodAsync(deviceId, "ModuleB", request);
return resp;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return await Task.FromResult(new MethodResponse(500));
}
}
The MethodB that I try to invoke from MethodA:
private static Task<MethodResponse> MethodB(MethodRequest methodRequest, object userContext)
{
Console.WriteLine("MethodB has been called");
// Get data but we do not do anything with it
var data = Encoding.UTF8.GetString(methodRequest.Data);
Console.WriteLine("Received data: " + data.ToString());
var methodResponse = new MethodResponse(Encoding.UTF8.GetBytes("{\"status\": \"ok\"}"), 200);
return Task.FromResult(methodResponse);
}
Note that I can invoke these two methods separately just fine from the azure portal or by using the Azure SDK for C# -> ServiceClient class.
My program crashes on the line
var resp = await ioTHubModuleClient.InvokeMethodAsync(deviceId, "ModuleB", request);
in MethodA and in the VS Code debugger I get the exception
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in System.Private.CoreLib.dll
And when I check the logs for my Message/StackTrace-print I get:
An error occurred while sending the request. at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.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.d__172.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.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 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at OrderGateway.Program.d__3.MoveNext() in /app/Program.cs:line 92
I've tried to initialize the ModuleClient using both Amqp with TCP/WebSocket_Only as well as Mqtt with TCP/WebSocket_Only. I've also tried to run the IoT Edge runtime with these modules on a Ubuntu 18.04 VM without any success.
Even when I try to invoke a method located in the same module with the ioTHubModuleClient.InvokeMethodAsync() I get the same exception / stacktrace...
I also tried to invoke the MethodB from the Init() function of ModuleA to try and not invoke a direct method in the context of a callback but I do get the same exception.
There is an open issue on github that you can find here: https://github.com/Azure/iotedge/issues/204
But it feels that it have kind of stalled and I don't get any help.
From my understanding this should be possible but I don't know what I'm missing?