How can we make asynchronous call to edge hub trigger from a C# edge module ?

Satyam Chauhan 492 Reputation points
2022-11-11T13:03:53.49+00:00

The iot edge solution is having 2 modules, first module is a C# module and second is Edgehub trigger module. First module is sending data in a loop to second module. I want the edge hub module to execute asynchronously

        static async Task Main(string[] args)  
                {  
                    _logger = Logger.Factory.CreateLogger
Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
377 questions
Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
531 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,456 Reputation points
    2022-11-15T20:28:06.483+00:00

    Hi @Satyam Chauhan , there are couple of different issues you are pointing here

    1. Make an asynchronous call to IoT Edge Hub trigger module -- We have already achieved this by removing the await key word before making the call to the "SendEventAsync". We
      noticed this evidently when we add a delay in the function, the Edge Hub trigger function gets invoked without waiting for the entire list to iterate. Please note that the C# module function
      does not wait any longer for the Edge Hub trigger function to return the Task and proceeds with iterating the list which indicates that the call is made asynchronously.
    2. Can the order of execution be changed on IoT Edge Hub trigger-- Please note that when we send out an event through SendEventAsyc method, the messages will be queued and
      passed along to the receiving end point. Please refer the below image that points to how SendEventAsyc function behaves.
      265386-screenshot-21.png
      Here is the Code Sample where you can find the above explanation of how the code behaves. Since the events are queued from the C# code module, the processing on the Edge Hub
      trigger will always be sequential.

    Hope this clarifies your question. Please let me know if you still have any doubts with regards to this issue.

    ----------

    Kindly accept answer or upvote if the response is helpful so that it would benefit other community members facing the same issue. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

  2. RAVENO SPANNEBERG 0 Reputation points
    2023-01-31T19:18:45.75+00:00

    static async Task SendMessageToEdgeHubModule(ModuleClient ioTHubModuleClient, List<String> items)

    {  
    
           await Parallel.ForEachAsync(items.ToList(), async (item, token) =>  
    
           {  
    
                    var messageOut = new Message(Encoding.ASCII.GetBytes(item));  
    
                    await ioTHubModuleClient.SendEventAsync("output1", messageOut);  
    
           }          
    
    }
    
    0 comments No comments