首页 > 解决方案 > Azure IoT Edge 模块直接方法响应显示为 [object Object]

问题描述

在特定模块上调用直接方法时,我只是[object Object]在 azure 门户中收到结果,我不知道自己做错了什么。请注意,当我使用 azure IoT SDK for c#(不运行 azure iot 运行时)执行完全相同的操作时,我正确收到了 JSON 对象,并且它不只是显示为 [object Object]。

请注意,我正在 c# 中开发它,并且 docker 容器(用于 IoT 边缘运行时及其模块)正在将 Linux 作为操作系统运行。
我有以下已注册为直接方法的示例方法。

在 iot edge 运行时 Init() 函数中,我执行以下操作:
await ioTHubModuleClient.SetMethodHandlerAsync("Sample1", Sample1, null);

示例方法如下所示:

private static Task<MethodResponse> Sample1(MethodRequest methodRequest, object userContext)
    {            
        // Get data but don't do anything with it... Works fine!
        var data = Encoding.UTF8.GetString(methodRequest.Data);

        var methodResponse = new MethodResponse(Encoding.UTF8.GetBytes("{\"status\": \"ok\"}"), 200);
        return Task.FromResult(methodResponse);
    }  

我可以通过在 Sample1 方法中设置断点来在调试模式下监控这个模块。我找不到我做错了什么?为什么从这个 Sample1 方法返回的响应只是显示为 [object Object],为什么{"status": "ok"}我没有像不使用 Azure IoT Edge 运行时那样看到 JSON 对象?

标签: azure-iot-hubazure-iot-sdkazure-iot-edge

解决方案


Direct Method 的回调结果是对象Task< MethodResponse > 。它不会序列化为 Json 字符串以显示在 Azure 门户中。但是您可以使用 Service Client Sdk 来获取回调响应,然后序列化为 JSON 字符串。

适用于 C#的最新Microsoft Azure IoT Hub SDK支持模块和 IoT Edge。您可以使用 SDK参考此示例。

更新:

在最新的 Azure IoT Hub SDK(Microsoft.Azure.Devices.Client 1.18)中,请使用 ModuleClinet 而不是 DeviceClient。可以参考以下模块中的代码。

namespace SampleModuleA
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Runtime.Loader;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Devices.Client.Transport.Mqtt;
    using Microsoft.Azure.Devices.Client;
    using Newtonsoft.Json;
    class Program
    {
        static int counter;

        static void Main(string[] args)
        {
            Init().Wait();

            // Wait until the app unloads or is cancelled
            var cts = new CancellationTokenSource();
            AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
            Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
            WhenCancelled(cts.Token).Wait();
        }

        /// <summary>
        /// Handles cleanup operations when app is cancelled or unloads
        /// </summary>
        public static Task WhenCancelled(CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource<bool>();
            cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).SetResult(true), tcs);
            return tcs.Task;
        }

        /// <summary>
        /// Initializes the ModuleClient and sets up the callback to receive
        /// messages containing temperature information
        /// </summary>
        static async Task Init()
        {
            MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_WebSocket_Only);
            ITransportSettings[] settings = { mqttSetting };

            // Open a connection to the Edge runtime
            ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
            await ioTHubModuleClient.OpenAsync();
            Console.WriteLine("[{0:HH:mm:ss ffff}]IoT Hub SampleModuleA client initialized.", DateTime.Now);

            await ioTHubModuleClient.SetMethodHandlerAsync("DirectMethod1", DirectMethod1, ioTHubModuleClient);

            // Register callback to be called when a message is received by the module
            await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
        }

        static async Task<MethodResponse> DirectMethod1(MethodRequest methodRequest, object moduleClient)
        {
            Console.WriteLine("Call DirectMethod1.");
            MethodResponse resp = null;

            //to do Something

            return resp;
        }

        /// <summary>
        /// This method is called whenever the module is sent a message from the EdgeHub. 
        /// It just pipe the messages without any change.
        /// It prints all the incoming messages.
        /// </summary>
        static async Task<MessageResponse> PipeMessage(Message message, object userContext)
        {
            int counterValue = Interlocked.Increment(ref counter);

            var moduleClient = userContext as ModuleClient;
            if (moduleClient == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
            }

            byte[] messageBytes = message.GetBytes();
            string messageString = Encoding.UTF8.GetString(messageBytes);
            Console.WriteLine($"Received message: {counterValue}, Body: [{messageString}]");

            if (!string.IsNullOrEmpty(messageString))
            {
                var pipeMessage = new Message(messageBytes);
                foreach (var prop in message.Properties)
                {
                    pipeMessage.Properties.Add(prop.Key, prop.Value);
                }
                await moduleClient.SendEventAsync("output1", pipeMessage);
                Console.WriteLine("Received message sent");
            }
            return MessageResponse.Completed;
        }
    }
}

推荐阅读