weixin_39637661
weixin_39637661
2020-12-27 14:37

Calling Direct Method issue with the auto-generated code on VSCode

  • OS, version, SKU and CPU architecture used:
  • Application's .NET Target Framework :
  • Device:
  • SDK version used:

Description of the issue:

[Calling Direct Method Issue] After several days of investigation and effort. I've found that when I am implementing IoT Edge Module by command-line in VSCode, calling Direct Method failed with an auto-generated code as below on VSCode. I had to change the code the other way to make it run properly. What they are different and what's MS's opinion.

The environment we use currently and see some similar issues below. IoT Edge version 1.0.7.1: https://github.com/Azure/azure-iotedge/releases • Fixes ObjectDisposedException (that we've seen also in our logs so it might be good to update the deployments to this version already)

IoT SDK C# version 1.20.2: https://github.com/Azure/azure-iot-sdk-csharp/releases • It's probably better to have the SDK updated to the latest release and start fixing the version instead of using 1.* which automatically uses the latest version at build time, which means we are not in control of the version we will get during the build.

Github reported issues similar to what we're seeing: • Issue #551: https://github.com/Azure/azure-iot-sdk-csharp/issues/571 • Issue #646: https://github.com/Azure/iotedge/issues/646

Code sample exhibiting the issue:

class Program { static async Task Main(string[] args) {

        // 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();//Auto generated code when creating new module
        await WhenCancelled(cts.Token);//Success with this code
    }

Console log of the issue:

[DirectMethod] Failed to invoke Direct Method: Error: {"Message":"{\"errorCode\":504101,\"trackingId\":\"8fb95f15eb734f5d92c292dc5fc29e17-G:19-TimeStamp:06/18/2019 10:07:22-G:11-TimeStamp:06/18/2019 10:07:22\",\"message\":\"Timed out waiting for the response from device.\",\"info\":{},\"timestampUtc\":\"2019-06-18T10:07:22.3918499Z\"}","ExceptionMessage":""}

该提问来源于开源项目:Azure/azure-iot-sdk-csharp

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39637661 weixin_39637661 4月前

    Hi In order to better understand the situation you met and resolve the issue, could you help update the sample project https://github.com/blackchoey/DirectMethodIssue980 so we can repro this issue? I have already invited you as collaborator. We haven't been able to repro this issue yet. In the meantime, could you share whether the problem was resolved after declared the main function as static async Task Main(string[] args) ?

    Yes, I edit the sample source you created to be fine and to reproduce. Thanks.

    点赞 评论 复制链接分享
  • weixin_39631445 weixin_39631445 4月前

    Moving this issue to the appropriate repository. Please continue to follow up on this issue: https://github.com/microsoft/vscode-azure-iot-edge/issues/480

    点赞 评论 复制链接分享
  • weixin_39710361 weixin_39710361 4月前

    , , , thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey

    点赞 评论 复制链接分享
  • weixin_39932692 weixin_39932692 4月前

    Do you have a link to the "VSCode" plugin that you're using? I don't think this is the right repo for the bug but we can help redirect it. FYI

    +1 on never mixing .Wait with await. This is known to cause deadlocks:

        //WhenCancelled(cts.Token).Wait();//Auto generated code when creating new module
        await WhenCancelled(cts.Token);//Success with this code
    
    点赞 评论 复制链接分享
  • weixin_39595164 weixin_39595164 4月前

    I believe this is the right repo - https://github.com/microsoft/vscode-azure-iot-edge

    点赞 评论 复制链接分享
  • weixin_39637661 weixin_39637661 4月前

    Let me add more detail codes as below. In async block with Wait() can occur deadlock is that what you mentioned? Thurs, as long as we changed "void Main" to "async Task Main" we should use await instead of using Wait(). Let me know if you have any unclear parts and if I move this request to another repository not here. Thanks.

    1.Auto-Generated code by VSCode when adding new IoT Edge Module by Command Palette class Program { 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(); } public static Task WhenCancelled(CancellationToken cancellationToken) { var tcs = new TaskCompletionSource(); cancellationToken.Register(s => ((TaskCompletionSource)s).SetResult(true), tcs); return tcs.Task; }

        **static async Task Init()**
        {
           //Some code
        }
    
    1. We modified codes base on the auto-generated and recommended codes by VSCode. class Program { static async Task Main(string[] args) { //Initialize the Device & Module Clients await InitDeviceClient(); await InitModuleClient();
          // Wait until the app unloads or is cancelled
          var cts = new CancellationTokenSource();
          AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
          Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
          **await WhenCancelled(cts.Token);**                      
      }
      public static Task WhenCancelled(CancellationToken cancellationToken)
      {
          var tcs = new TaskCompletionSource<bool>();
          cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).SetResult(true), tcs);
          return tcs.Task;
      }
      static async Task InitDeviceClient()
      {
          //Some codes
      }
      static async Task InitModuleClient()
      {
          //Some codes
      }
      
    点赞 评论 复制链接分享
  • weixin_39516956 weixin_39516956 4月前

    Hi In order to better understand the situation you met and resolve the issue, could you help update the sample project https://github.com/blackchoey/DirectMethodIssue980 so we can repro this issue? I have already invited you as collaborator. We haven't been able to repro this issue yet. In the meantime, could you share whether the problem was resolved after declared the main function as static async Task Main(string[] args) ?

    点赞 评论 复制链接分享

相关推荐