Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Dolphinsimon opened this issue Oct 17, 2023 · 8 comments
Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board

Comments

@Dolphinsimon
Copy link

Dolphinsimon commented Oct 17, 2023

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

@Dolphinsimon Dolphinsimon added the bug Something isn't working. label Oct 17, 2023
@github-actions github-actions bot added the IoTSDK Tracks all IoT SDK issues across the board label Oct 17, 2023
@rido-min
Copy link
Member

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

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

@Dolphinsimon
Copy link
Author

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
Copy link
Author

Dolphinsimon commented Oct 18, 2023

I have updated the codes with Github code blocks.

@Dolphinsimon
Copy link
Author

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

@rido-min
Copy link
Member

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
Copy link
Author

Dolphinsimon commented Oct 19, 2023

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?

@rido-min
Copy link
Member

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board
Projects
None yet
Development

No branches or pull requests

2 participants