Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
463 stars 493 forks source link

[Bug Report] Cannot invoke direct methods with payload in IotEdge devices. #3381

Open Dolphinsimon opened 10 months ago

Dolphinsimon commented 10 months ago

Context

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."}

rido-min commented 10 months ago

seems the quotes in the json strings are not properly escaped:

string json =
"{\"schemaVersion\": \"1.0\" ...
Dolphinsimon commented 10 months ago

seems the quotes in the json strings are not properly escaped:

string json =
"{\"schemaVersion\": \"1.0\" ...

The quotes are escaped in VS, seems the trailing slash are removed by Github. If the quotes are not escaped, the code won't be compiled. 2023-10-18 091819

Dolphinsimon commented 10 months ago

I have updated the codes with Github code blocks.

Dolphinsimon commented 10 months ago

If the DirectMethodServiceRequest payload is null(the ping direct method), the result will be 200.

rido-min commented 10 months ago

Hi @Dolphinsimon

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());
Dolphinsimon commented 10 months ago

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:

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?

rido-min commented 10 months ago

@Dolphinsimon

Yes, this seems like a bug in v2 previews.

May I ask what's your motivation to use v2-previews instead of v1 releases?

Dolphinsimon commented 10 months ago

@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.