[Bug Report] Cannot invoke direct methods with payload in IotEdge devices.
Dolphinsimon opened this issue · 8 comments
Context
- **OS, version, SKU and CPU architecture used:Windows 10 Desktop x64
- **Application's .NET Target Framework : .NET8.0-RC2
- **Device: Laptop
- **SDK version used: 2.0.0-preview007
- IoT Edge version: 1.4.23
Description of the issue
Cannot invoke direct methods with payload in IotEdge devices.
Code sample exhibiting the issue
var serviceClient = new IotHubServiceClient("HostName=***");
string json =
"{\"schemaVersion\": \"1.0\",\"items\": [{\"id\": \"module\",\"filter\": {\"tail\": 3,\"since\": \"10m\"}}],\"encoding\": \"none\",\"contentType\": \"text\"}";
DirectMethodServiceRequest request = new("GetModuleLogs")
{
Payload = Encoding.UTF8.GetBytes(json)
};
var result = await serviceClient.DirectMethods.InvokeAsync("deviceId", "$edgeAgent", request);
Console log of the issue
{"message":"Error parsing command payload because of error - Error converting value "eyJzY2hlbWFWZXJzaW9uIjogIjEuMCIsIml0ZW1zIjogW3siaWQiOiAiaW90ZWRnZWdhdGV3YXkiLCJmaWx0ZXIiOiB7InRhaWwiOiAzLCJzaW5jZSI6ICIxMG0ifX1dLCJlbmNvZGluZyI6ICJub25lIiwiY29udGVudFR5cGUiOiAidGV4dCJ9" to type 'Microsoft.Azure.Devices.Edge.Agent.Core.Requests.ModuleLogsRequest'. Path '', line 1, position 186."}
seems the quotes in the json strings are not properly escaped:
string json =
"{\"schemaVersion\": \"1.0\" ...
I have updated the codes with Github code blocks.
If the DirectMethodServiceRequest payload is null(the ping direct method), the result will be 200.
Thanks for reporting this issue, while we investigate can you try with the non-preview version?
https://www.nuget.org/packages/Microsoft.Azure.Devices/1.39.0
var serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
string json = """
{
"schemaVersion": "1.0",
"items": [
{
"id": "edgeAgent",
"filter": {
"tail": 3,
"since": "10m",
}
}
],
"encoding": "none",
"contentType": "text"
}
""";
CloudToDeviceMethod request = new("GetModuleLogs");
request.SetPayloadJson(json);
var result = await serviceClient.InvokeDeviceMethodAsync("deviceId", "$edgeAgent", request);
Console.WriteLine(result.Status);
Console.WriteLine(result.GetPayloadAsJson());
Hi @rido-min
I have tested the stable version 1.39.0, the direct method can be invoked successfully.
I have made some more tests with server - client sdk with different versions:
-
Server with stable version 1.39.0 and Client with stable version 1.42.0 or 2.0.0-preview007
Direct methods are always invoked successfully. -
Server with preview version 2.0.0-preview007
Client with preview version 2.0.0-preview007: Direct methods can be invoked successfully.
Client with stable version 1.42.0: Direct methods failed if deserialize MethodRequest data.
Seems there are breaking changes in server direct method payload encoder in preview version sdk makes the client must have preview version to work.
Is the behavior in preview sdk by design? Or is it a bug?
Yes, this seems like a bug in v2 previews.
May I ask what's your motivation to use v2-previews instead of v1 releases?
@rido-min
If this is a bug, is there an ETA to fix it?
The main reason for us to try the preview v2 is the simplified service client model IotHubServiceClient.