azure - 如何从另一个模块调用物联网边缘代理上的直接方法?

标签 azure iot azure-iot-hub azure-iot-edge

我正在尝试 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, Func3modifyRequestMessageAsync, Func2 isSuccessful, Func3 processResponseMessageAsync, IDictionary2 errorMappingOverrides , CancellationToken 取消 token ) 在 Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.PostAsync[T1,T2](Uri requestUri,T1 实体,IDictionary2 errorMappingOverrides,IDictionary2 customHeaders,CancellationToken CancellationToken) 在 Microsoft.Azure.Devices.Client.ModuleClient.InvokeMethodAsync(Uri uri, MethodRequest methodRequest, CancellationToken CancellationToken)

最佳答案

这是预期的行为,其他模块无法调用边缘代理的直接方法。边缘代理在设计上并未连接到edgeHub,因为代理的直接方法具有特权并且需要服务信用才能访问。

关于azure - 如何从另一个模块调用物联网边缘代理上的直接方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63806134/

相关文章:

azure - 使用 IoT 中心实例化 DeviceClient

azure - 无法为 Event-Grid 创建 Webhook 订阅

keyboard - 有人让 USB 在 Raspberry Pi 2 上的 Windows IoT 中工作吗?

azure - 无法创建 IoT 中心

c++ - Libmosquitto 发布不会将所有消息传递到 Azure IoT 中心

python - 尝试使用自定义镜像创建镜像时,Azure python vm create 失败

php - Azure Web 应用程序中的重写规则出现权限错误

azure - ESP8266 未连接到 Azure Iot Hub,返回状态 -2

python-3.x - 如何使用 python 在 paho mqtt 中使用 ssl/tls?我得到证书验证失败

azure - 使用自定义程序运行 Windows 10 Core 的 Raspberry Pi 3 显示划掉的 x