Reputation: 1741
I'm playing around with experimental features from 1.0.10-rc2.
There is a direct method on edgeAgent UploadModuleLogs.
I can call the method from Azure Portal and it works fine. I'm trying to call the method from my module on the same device like this:
static async Task Init()
{
...
await ioTHubModuleClient.SetMethodHandlerAsync("UploadModuleLogs", UploadModuleLogs, ioTHubModuleClient);
}
private static async Task<MethodResponse> UploadModuleLogs(MethodRequest methodRequest, object userContext)
{
ModuleClient ioTHubModuleClient = (ModuleClient)userContext;
// Upload logs
Console.WriteLine("Invoking method On edgeAgent");
var payload = @"{
""schemaVersion"": ""1.0"",
""sasUrl"": ""https://..."",
""items"": [
{
""id"": "".*"",
""filter"": {
""tail"": 100
}
}
],
""encoding"": ""none"",
""contentType"": ""text""
} ";
MethodRequest request = new MethodRequest("UploadModuleLogs", Encoding.UTF8.GetBytes(payload));
string myModuleId = Environment.GetEnvironmentVariable("IOTEDGE_MODULEID");
string myDeviceId = Environment.GetEnvironmentVariable("IOTEDGE_DEVICEID");
Console.WriteLine($"{myDeviceId} {myModuleId} - UploadModuleLogs method request at {DateTime.Now}");
//Console.WriteLine($"{request.DataAsJson}");
try
{
var response = await ioTHubModuleClient.InvokeMethodAsync(myDeviceId, "$edgeAgent", request).ConfigureAwait(false);
Console.WriteLine($"Received response with status: {response.Status} with message: {response.ResultAsJson}");
return response;
}
catch (Exception ex)
{
Console.WriteLine($"Error invoking method {ex}");
}
return null;
Trying to run this:
Error invoking method Microsoft.Azure.Devices.Client.Exceptions.DeviceNotFoundException: Device {"message":"Client myDeviceName/$edgeAgent not found"} not registered
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.ExecuteAsync(HttpMethod httpMethod, Uri requestUri, Func3 modifyRequestMessageAsync, Func
2 isSuccessful, Func3 processResponseMessageAsync, IDictionary
2 errorMappingOverrides, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.PostAsync[T1,T2](Uri requestUri, T1 entity, IDictionary2 errorMappingOverrides, IDictionary
2 customHeaders, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.ModuleClient.InvokeMethodAsync(Uri uri, MethodRequest methodRequest, CancellationToken cancellationToken)
Upvotes: 2
Views: 1996
Reputation: 151
for module to module communication you should create messages and route from one to the other https://learn.microsoft.com/en-us/azure/iot-edge/module-composition#declare-routes.
Methods are not supported :-(
Setting up a route allows you to call a method on another module which is connected to the same edgeHub:
FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint("/modules/SimulatedTemperatureSensor/inputs/control")
Upvotes: 1
Reputation: 558
This is expected behavior, other modules cannot invoke edge agent's direct methods. The edge agent does not connect to edgeHub by design as the agent's direct methods are privileged and need service creds to access.
Upvotes: 1