如何从另一个模块调用物联网边缘代理的直接方法?
How to call direct method on iot edge agent from another module?
我正在尝试 1.0.10-rc2 中的实验性功能。
edgeAgent UploadModuleLogs 上有一个直接的方法。
我可以从 Azure 门户调用该方法,它工作正常。
我正在尝试从我的模块在同一设备上调用该方法,如下所示:
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;
正在尝试 运行 这个:
调用方法时出错 Microsoft.Azure.Devices.Client.Exceptions.DeviceNotFoundException:设备 {"message":"Client myDeviceName/$edgeAgent not found"} 未注册
在 Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.ExecuteAsync(HttpMethod httpMethod,Uri requestUri,Func3 modifyRequestMessageAsync, Func
2 isSuccessful,Func3 processResponseMessageAsync, IDictionary
2 errorMappingOverrides,CancellationToken cancellationToken)
在 Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.PostAsync[T1,T2](Uri requestUri, T1 实体, IDictionary2 errorMappingOverrides, IDictionary
2 customHeaders, CancellationToken cancellationToken)
在 Microsoft.Azure.Devices.Client.ModuleClient.InvokeMethodAsync(Uri uri, MethodRequest methodRequest, CancellationToken cancellationToken)
对于模块到模块的通信,您应该创建消息并从一个模块路由到另一个模块https://docs.microsoft.com/en-us/azure/iot-edge/module-composition#declare-routes。
不支持方法:-(
设置路由允许您在连接到同一个 edgeHub 的另一个模块上调用方法:
FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint("/modules/SimulatedTemperatureSensor/inputs/control")
这是预期的行为,其他模块无法调用边缘代理的直接方法。由于代理的直接方法具有特权并且需要服务凭据才能访问,因此边缘代理不会按设计连接到 edgeHub。
我正在尝试 1.0.10-rc2 中的实验性功能。
edgeAgent UploadModuleLogs 上有一个直接的方法。
我可以从 Azure 门户调用该方法,它工作正常。 我正在尝试从我的模块在同一设备上调用该方法,如下所示:
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;
正在尝试 运行 这个:
调用方法时出错 Microsoft.Azure.Devices.Client.Exceptions.DeviceNotFoundException:设备 {"message":"Client myDeviceName/$edgeAgent not found"} 未注册
在 Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.ExecuteAsync(HttpMethod httpMethod,Uri requestUri,Func3 modifyRequestMessageAsync, Func
2 isSuccessful,Func3 processResponseMessageAsync, IDictionary
2 errorMappingOverrides,CancellationToken cancellationToken)
在 Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.PostAsync[T1,T2](Uri requestUri, T1 实体, IDictionary2 errorMappingOverrides, IDictionary
2 customHeaders, CancellationToken cancellationToken)
在 Microsoft.Azure.Devices.Client.ModuleClient.InvokeMethodAsync(Uri uri, MethodRequest methodRequest, CancellationToken cancellationToken)
对于模块到模块的通信,您应该创建消息并从一个模块路由到另一个模块https://docs.microsoft.com/en-us/azure/iot-edge/module-composition#declare-routes。
不支持方法:-(
设置路由允许您在连接到同一个 edgeHub 的另一个模块上调用方法:
FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint("/modules/SimulatedTemperatureSensor/inputs/control")
这是预期的行为,其他模块无法调用边缘代理的直接方法。由于代理的直接方法具有特权并且需要服务凭据才能访问,因此边缘代理不会按设计连接到 edgeHub。